Skip to content

Commit 4a4e928

Browse files
committed
add "Data Source" example
1 parent e3149c7 commit 4a4e928

File tree

11 files changed

+144
-12
lines changed

11 files changed

+144
-12
lines changed

src/Modules/XamlDesigner/ViewModels/DesignerControlViewModel.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,6 @@ private void OnRefreshDesigner(TabInfo tabInfo)
183183

184184
private void OnSyncDataSource(string jsonString)
185185
{
186-
if(IsReadOnly)
187-
return;
188-
189186
try
190187
{
191188
_dataSource = string.IsNullOrWhiteSpace(jsonString) ? null : JsonUtil.DeserializeObject(jsonString);

src/XamlTheme/Controls/HyperLink.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Windows;
44
using System.Windows.Controls;
55
using System.Windows.Input;
6+
using System.ComponentModel;
67

78
namespace XamlTheme.Controls
89
{
@@ -18,6 +19,7 @@ public Hyperlink()
1819

1920
public static readonly DependencyProperty NavigateUriProperty =
2021
DependencyProperty.Register("NavigateUri", typeof(Uri), typeof(Hyperlink), new PropertyMetadata(null, OnNavigateUriPropertyChanged));
22+
[TypeConverter(typeof(UriTypeConverter))]
2123
public Uri NavigateUri
2224
{
2325
get { return (Uri)GetValue(NavigateUriProperty); }

src/XamlTheme/Utils/Converters.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
4545
}
4646
}
4747

48+
public class NullToVisibilityConverter : IValueConverter
49+
{
50+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
51+
{
52+
var result = (Visibility)(parameter ?? Visibility.Visible);
53+
54+
if(value == null || value == DependencyProperty.UnsetValue)
55+
return result;
56+
57+
return result == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
58+
}
59+
60+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
61+
{
62+
throw new NotImplementedException();
63+
}
64+
}
65+
4866
public class DoubleToVisibilityConverter : IValueConverter
4967
{
5068
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

src/XamlViewer/App.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<Application.Resources>
66
<ResourceDictionary>
77
<ResourceDictionary.MergedDictionaries>
8+
<ResourceDictionary Source="Resources/Examples/DataSourceExample.xaml"/>
89
<ResourceDictionary Source="Resources/Examples/AnimationExample.xaml"/>
910
<ResourceDictionary Source="Resources/Examples/CustomControlExample.xaml"/>
1011
<ResourceDictionary Source="Resources/Examples/StyleExample.xaml"/>

src/XamlViewer/App.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ private void App_DispatcherUnhandledException(object sender, DispatcherUnhandled
4848
var time = " [" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff") + "]";
4949

5050
MessageBox.Show(msg, "Exception" + time, MessageBoxButton.OK, MessageBoxImage.Error);
51+
52+
e.Handled = true;
5153
}
5254

5355
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)

src/XamlViewer/Models/InternalConstStrings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ internal static class InternalConstStrings
2727
public static string AnimationExampleFileName { get { return "AnimationExample.xaml"; } }
2828
public static string AnimationExampleFileContentKey { get { return "AnimationExampleFileContentKey"; } }
2929

30+
//data source example
31+
public static string DataSourceExampleFileName { get { return "DataSourceExample.xaml"; } }
32+
public static string DataSourceExampleFileContentKey { get { return "DataSourceExampleFileContentKey"; } }
33+
3034
//custom control example
3135
public static string CustomControlExampleFileName { get { return "CustomControlExample.xaml"; } }
3236
public static string CustomControlExampleFileContentKey { get { return "CustomControlExampleFileContentKey"; } }

src/XamlViewer/Models/ResourcesMap.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public static class ResourcesMap
2424
[0] = InternalConstStrings.StyleExampleFileName,
2525
[1] = InternalConstStrings.WindowExampleFileName,
2626
[2] = InternalConstStrings.AnimationExampleFileName,
27-
[3] = InternalConstStrings.CustomControlExampleFileName,
27+
[3] = InternalConstStrings.DataSourceExampleFileName,
28+
[4] = InternalConstStrings.CustomControlExampleFileName,
2829
};
2930

3031
public static Dictionary<string, string> NameToContentKeyDic = new Dictionary<string, string>
@@ -33,6 +34,7 @@ public static class ResourcesMap
3334
[InternalConstStrings.StyleExampleFileName] = InternalConstStrings.StyleExampleFileContentKey,
3435
[InternalConstStrings.WindowExampleFileName] = InternalConstStrings.WindowExampleFileContentKey,
3536
[InternalConstStrings.AnimationExampleFileName] = InternalConstStrings.AnimationExampleFileContentKey,
37+
[InternalConstStrings.DataSourceExampleFileName] = InternalConstStrings.DataSourceExampleFileContentKey,
3638
[InternalConstStrings.CustomControlExampleFileName] = InternalConstStrings.CustomControlExampleFileContentKey,
3739
};
3840
}

src/XamlViewer/Resources/Constants.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@
8181
<controls:Hyperlink Text="Style" Margin="0,15,0,0" Tag="0"/>
8282
<controls:Hyperlink Text="Window" Margin="0,10,0,0" Tag="1"/>
8383
<controls:Hyperlink Text="Animation" Margin="0,10,0,0" Tag="2"/>
84-
<controls:Hyperlink Text="Third-Party Controls" Margin="0,10,0,0" Tag="3"/>
84+
<controls:Hyperlink Text="Data Source" Margin="0,10,0,0" Tag="3"/>
85+
<controls:Hyperlink Text="Third-Party Controls" Margin="0,10,0,0" Tag="4"/>
8586
</StackPanel>
8687
8788
</StackPanel>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:sys="clr-namespace:System;assembly=mscorlib"
4+
xmlns:models="clr-namespace:XamlViewer.Models">
5+
6+
<sys:String x:Key="{x:Static models:InternalConstStrings.DataSourceExampleFileContentKey}" xml:space="preserve">
7+
<![CDATA[<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
8+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
9+
xmlns:controls="clr-namespace:XamlTheme.Controls;assembly=XamlTheme"
10+
xmlns:utils="clr-namespace:XamlTheme.Utils;assembly=XamlTheme"
11+
xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
12+
Background="#282C34"
13+
TextBlock.Foreground="#CCCCCC"
14+
TextBlock.FontFamily="YaHei Consolas HyBrid">
15+
16+
<!--Welcome-->
17+
<mwt:SystemDropShadowChrome HorizontalAlignment="Center" VerticalAlignment="Center">
18+
<mwt:SystemDropShadowChrome.Visibility>
19+
<Binding Path="DataContext" ElementName="ReleaseItemsControl">
20+
<Binding.Converter>
21+
<utils:NullToVisibilityConverter/>
22+
</Binding.Converter>
23+
</Binding>
24+
</mwt:SystemDropShadowChrome.Visibility>
25+
<Border Background="#21252B" Padding="30" CornerRadius="5">
26+
<StackPanel TextBlock.FontSize="14">
27+
<TextBlock Text="Welcome" HorizontalAlignment="Center" FontSize="20"/>
28+
<TextBlock Margin="0,20,0,0" Text="> Open [ "><Run Foreground="#E37A39">Data Source</Run> ] panel;</TextBlock>
29+
<TextBlock Margin="0,10,0,0" Text="> Copy and paste follow api into [ "><Run Foreground="#E37A39">Rest API</Run> ] text box;</TextBlock>
30+
<TextBox Height="25" Foreground="Gray" Background="Transparent" IsReadOnly="True" FontSize="14" BorderThickness="0" Margin="22,10,0,0"
31+
Text="https://api.github.com/repos/huangjia2107/xamlviewer/releases"/>
32+
<TextBlock Margin="0,10,0,0" Text="> Click [ "><Run Foreground="#E37A39">Fetch</Run> ] button;</TextBlock>
33+
<TextBlock Margin="0,10,0,0" Text="> Turn on [ "><Run Foreground="#E37A39">Bind</Run> ] switch.</TextBlock>
34+
</StackPanel>
35+
</Border>
36+
</mwt:SystemDropShadowChrome>
37+
38+
<!--Release Note-->
39+
<Grid>
40+
<Grid.Visibility>
41+
<Binding Path="DataContext" ElementName="ReleaseItemsControl"
42+
ConverterParameter="{x:Static Visibility.Collapsed}">
43+
<Binding.Converter>
44+
<utils:NullToVisibilityConverter/>
45+
</Binding.Converter>
46+
</Binding>
47+
</Grid.Visibility>
48+
<Grid.RowDefinitions>
49+
<RowDefinition Height="80"/>
50+
<RowDefinition Height="*"/>
51+
</Grid.RowDefinitions>
52+
<TextBlock Text="Release History" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
53+
<ItemsControl Grid.Row="1" x:Name="ReleaseItemsControl" Margin="100,0" ItemsSource="{Binding .}">
54+
<ItemsControl.ItemTemplate>
55+
<DataTemplate>
56+
<mwt:SystemDropShadowChrome Margin="0,0,0,30">
57+
<Border Background="#21252B" BorderBrush="DarkGray" BorderThickness="0" Padding="20,15" CornerRadius="5">
58+
<StackPanel>
59+
<Grid Height="30" TextBlock.FontWeight="Bold">
60+
<Grid.ColumnDefinitions>
61+
<ColumnDefinition Width="auto"/>
62+
<ColumnDefinition Width="auto"/>
63+
<ColumnDefinition Width="*"/>
64+
<ColumnDefinition Width="auto"/>
65+
</Grid.ColumnDefinitions>
66+
<Ellipse Height="15" Width="15" Fill="Green" Margin="0,0,10,0"/>
67+
<TextBlock Grid.Column="1" Text="{Binding name,StringFormat=v{0}}" VerticalAlignment="Center"/>
68+
<TextBlock Grid.Column="3" Text="{Binding published_at,StringFormat={}{0:yyyy-MM-dd&#x0020;HH:mm:ss}}" VerticalAlignment="Center"/>
69+
</Grid>
70+
<Path RenderOptions.EdgeMode="Aliased" Stroke="#27313A" StrokeThickness="1" Data="M0,0 H10" Stretch="Fill" Margin="0,10,0,0"/>
71+
<ItemsControl ItemsSource="{Binding assets}" Grid.IsSharedSizeScope="true">
72+
<ItemsControl.ItemTemplate>
73+
<DataTemplate>
74+
<Grid Margin="0,15,0,0">
75+
<Grid.ColumnDefinitions>
76+
<ColumnDefinition Width="auto" SharedSizeGroup="NameColumn"/>
77+
<ColumnDefinition Width="*"/>
78+
<ColumnDefinition Width="auto"/>
79+
</Grid.ColumnDefinitions>
80+
<TextBlock Text="{Binding name,StringFormat={}{0}}" VerticalAlignment="Center"/>
81+
<TextBlock Grid.Column="1" Text="{Binding download_count,StringFormat={}{0}}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
82+
<controls:Hyperlink Grid.Column="2" Text="Download"
83+
VerticalAlignment="Center"
84+
NavigateUri="{Binding browser_download_url}"
85+
ToolTip="{Binding NavigateUri,RelativeSource={RelativeSource Self}}"/>
86+
</Grid>
87+
</DataTemplate>
88+
</ItemsControl.ItemTemplate>
89+
</ItemsControl>
90+
<Path RenderOptions.EdgeMode="Aliased" Stroke="#27313A" StrokeThickness="1" Data="M0,0 H10" Stretch="Fill" Margin="0,15"/>
91+
<TextBlock Text="{Binding body}" LineHeight="20" Foreground="#91A7BE"/>
92+
</StackPanel>
93+
</Border>
94+
</mwt:SystemDropShadowChrome>
95+
</DataTemplate>
96+
</ItemsControl.ItemTemplate>
97+
</ItemsControl>
98+
</Grid>
99+
</Grid>]]>
100+
</sys:String>
101+
102+
</ResourceDictionary>

src/XamlViewer/ViewModels/DataViewModel.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public DataViewModel(IContainerExtension container, IEventAggregator eventAggreg
4040

4141
InitEvent();
4242
InitCommand();
43-
InitStatus();
4443
}
4544

4645
#region Init
@@ -59,12 +58,6 @@ private void InitCommand()
5958
RequestCommand = new DelegateCommand(Request);
6059
}
6160

62-
private void InitStatus()
63-
{
64-
if (IsSyncDataSource)
65-
UpdateDataSource(true);
66-
}
67-
6861
#endregion
6962

7063
#region Event
@@ -89,6 +82,11 @@ private void Loaded(TextEditorEx textEditor)
8982
_textEditor.LoadSyntaxHighlighting(AppDomain.CurrentDomain.BaseDirectory + "Assets\\Json.xshd");
9083
_textEditor.Text = JsonString;
9184
}
85+
86+
UpdateByJsonString();
87+
88+
if (IsSyncDataSource)
89+
UpdateDataSource(true);
9290
}
9391

9492
private void DelayArrived()

0 commit comments

Comments
 (0)