Skip to content

Commit

Permalink
Improve the basics of demo project.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoYang06 committed Sep 7, 2021
1 parent 49b9ced commit 1a53091
Show file tree
Hide file tree
Showing 19 changed files with 547 additions and 135 deletions.
10 changes: 10 additions & 0 deletions Source/Demos/Wif.Demo.Common/Interfaces/IActivatable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Wif.Demo.Common.Interfaces
{
/// <summary>
/// Use this Interface when you want to mark a control as recieving View
/// Activation when it doesn't have a backing ViewModel.
/// </summary>
public interface IActivatable
{
}
}
10 changes: 10 additions & 0 deletions Source/Demos/Wif.Demo.Common/Interfaces/IActivatableView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Wif.Demo.Common.Interfaces
{
/// <summary>
/// Use this Interface when you want to mark a control as receiving View
/// Activation when it doesn't have a backing ViewModel.
/// </summary>
public interface IActivatableView
{
}
}
30 changes: 30 additions & 0 deletions Source/Demos/Wif.Demo.Common/Interfaces/IViewFor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Wif.Demo.Common.Interfaces
{
/// <summary>
/// This base class is mostly used by the Framework. Implement <see cref="IViewFor{T}"/>
/// instead.
/// </summary>
public interface IViewFor : IActivatable
{
/// <summary>
/// Gets or sets the View Model associated with the View.
/// </summary>
object ViewModel { get; set; }
}

#pragma warning disable SA1402 // File may only contain a single type
/// <summary>
/// Implement this interface on your Views to support Routing and Binding.
/// </summary>
/// <typeparam name="T">The type of ViewModel.</typeparam>
public interface IViewFor<T> : IViewFor
#pragma warning restore SA1402 // File may only contain a single type
where T : class
{
/// <summary>
/// Gets or sets the ViewModel corresponding to this specific View. This should be
/// a DependencyProperty if you're using XAML.
/// </summary>
new T ViewModel { get; set; }
}
}
42 changes: 42 additions & 0 deletions Source/Demos/Wif.Demo.Common/UserControlBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Windows;
using System.Windows.Controls;
using Wif.Demo.Common.Interfaces;

namespace Wif.Demo.Common
{
public class UserControlBase<TViewModel> : UserControl, IViewFor<TViewModel> where TViewModel : class
{
/// <summary>
/// The view model dependency property.
/// </summary>
public static readonly DependencyProperty ViewModelProperty =
DependencyProperty.Register(nameof(ViewModel), typeof(TViewModel), typeof(UserControlBase<TViewModel>), new PropertyMetadata(null));

/// <summary>
/// Gets the binding root view model.
/// </summary>
public TViewModel BindingRoot => ViewModel;

#region IViewFor<TViewModel> Members

/// <summary>
///
/// </summary>
public TViewModel ViewModel
{
get => (TViewModel)GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}

/// <summary>
///
/// </summary>
object IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (TViewModel)value;
}

#endregion
}
}
8 changes: 3 additions & 5 deletions Source/Demos/Wif.Demo/App.xaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<Application x:Class="Wif.Demo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Wif.Demo"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
Exit="App_OnExit"
Startup="App_OnStartup">
<Application.Resources />
</Application>
132 changes: 131 additions & 1 deletion Source/Demos/Wif.Demo/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
using System;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Wif.Demo.IocExtensions;

namespace Wif.Demo
{
Expand All @@ -13,5 +20,128 @@ namespace Wif.Demo
/// </summary>
public partial class App : Application
{
#region Private Fields

/// <summary>
/// 定义程序当前的UI调度器。
/// </summary>
public Dispatcher CurrentUIDispatcher { get; private set; }

#endregion Private Fields

#region Constructors

/// <summary>
/// Initializes a new instance of the <see cref="App" /> class.
/// </summary>
public App()
{
InitHostBuilder();
CurrentUIDispatcher = Current.Dispatcher;
}

#endregion Constructors

#region Fields

/// <summary>
/// 定义通用主机字段。
/// </summary>
private IHost _host;

/// <summary>
/// 获取或设置依赖注入的容器。
/// </summary>
public IServiceProvider Container { get; private set; }

#endregion Fields

#region Methods

/// <summary>
/// UI线程抛出全局异常事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[HandleProcessCorruptedStateExceptions]
private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
try
{
MessageBox.Show($"UI线程全局异常\r\n{e.Exception}", "警告");
e.Handled = true;
}
catch (Exception ex)
{
MessageBox.Show("应用程序发生不可恢复的异常,将要退出!", "警告");
}
}

/// <summary>
/// The App_OnExit
/// </summary>
/// <param name="sender">The sender <see cref="object" /></param>
/// <param name="e">The e <see cref="ExitEventArgs" /></param>
private async void App_OnExit(object sender, ExitEventArgs e)
{
using (_host)
{
await _host.StopAsync(TimeSpan.FromSeconds(1));
}
}

/// <summary>
/// The App_OnStartup
/// </summary>
/// <param name="sender">The sender <see cref="object" /></param>
/// <param name="e">The e <see cref="StartupEventArgs" /></param>
private async void App_OnStartup(object sender, StartupEventArgs e)
{
// 注册异常事件。
Current.DispatcherUnhandledException += App_OnDispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

await _host.StartAsync();

var mainWindow = _host.Services.GetRequiredService<MainWindow>();
mainWindow.Show();
}

/// <summary>
/// 非UI线程抛出全局异常事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
[HandleProcessCorruptedStateExceptions]
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
if (e.ExceptionObject is not Exception ex)
return;
CurrentUIDispatcher?.InvokeAsync(() => { MessageBox.Show($"非UI线程全局异常\r\n{ex.Message}"); });
}
catch (Exception ex)
{
CurrentUIDispatcher?.InvokeAsync(() => { MessageBox.Show("应用程序发生不可恢复的异常,将要退出!"); });
}
}

/// <summary>
/// 创建和配置构建器对象。
/// </summary>
private void InitHostBuilder()
{
// 初始化依赖注入
_host = new HostBuilder()
.ConfigureView()
.UseEnvironment(Environments.Development)
.Build();

Container = _host.Services;
Locator.SetLocator(Container);
}

#endregion Methods
}
}
70 changes: 70 additions & 0 deletions Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<common:UserControlBase x:TypeArguments="bindingDemo:BindingDemoViewModel"
x:Class="Wif.Demo.Examples.BindingDemo.BindingDemoView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:common="clr-namespace:Wif.Demo.Common;assembly=Wif.Demo.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:bindingDemo="clr-namespace:Wif.Demo.Examples.BindingDemo"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d" >
<UserControl.Resources>
<DataTemplate x:Key="Brand"
DataType="common:MobilePhone">
<TextBox x:Name="txbBrand"
Text="{Binding Brand}" />
</DataTemplate>
<DataTemplate x:Key="NumberOfCPUCore"
DataType="common:MobilePhone">
<TextBox x:Name="txbNumberOfCPUCore"
Text="{Binding NumberOfCPUCore}" />
</DataTemplate>
<DataTemplate x:Key="RAM"
DataType="common:MobilePhone">
<TextBox x:Name="txbRAM"
Text="{Binding RAM}" />
</DataTemplate>
<DataTemplate x:Key="ROM"
DataType="common:MobilePhone">
<TextBox Name="txbROM"
Text="{Binding ROM}" />
</DataTemplate>
<DataTemplate x:Key="ScreenResolution"
DataType="common:MobilePhone">
<TextBox x:Name="txbScreenResolution"
Text="{Binding ScreenResolution}" />
</DataTemplate>
<DataTemplate x:Key="ScreenSize"
DataType="common:MobilePhone">
<TextBox x:Name="txbScreenSize"
Text="{Binding ScreenSize, Mode=TwoWay}" />
</DataTemplate>

<Style TargetType="TextBox">
<Setter Property="Padding"
Value="9 5" />
</Style>
</UserControl.Resources>
<Grid>
<ListView x:Name="ListViewMobiePhone"
ItemsSource="{Binding MobilePhoneCollection}">
<ListView.View>
<GridView>
<GridViewColumn CellTemplate="{StaticResource Brand}"
Header="品牌" />
<GridViewColumn CellTemplate="{StaticResource NumberOfCPUCore}"
Header="CPU核数" />
<GridViewColumn CellTemplate="{StaticResource RAM}"
Header="运行内存" />
<GridViewColumn CellTemplate="{StaticResource ROM}"
Header="机身内存" />
<GridViewColumn CellTemplate="{StaticResource ScreenResolution}"
Header="屏幕分辨率" />
<GridViewColumn CellTemplate="{StaticResource ScreenSize}"
Header="屏幕尺寸 " />
</GridView>
</ListView.View>
</ListView>
</Grid>
</common:UserControlBase>
28 changes: 28 additions & 0 deletions Source/Demos/Wif.Demo/Examples/BindingDemo/BindingDemoView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Wif.Demo.Examples.BindingDemo
{
/// <summary>
/// BindingDemoView.xaml 的交互逻辑
/// </summary>
public partial class BindingDemoView
{
public BindingDemoView()
{
InitializeComponent();
}
}
}
Loading

0 comments on commit 1a53091

Please sign in to comment.