Commit a74a4562 authored by 姜春辉's avatar 姜春辉

视图模型Handler修改为自动加载,不再需要手动注册

parent 341c9132
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GS.Terminal.VisitorSelfService.Logic.Core
{
public class AuthenticationPageHandler : Corebase<ViewModels.Pages.AuthenticationPage.ViewModel>
{
public override void Init()
{
VM.OnExitClick += VM_OnExitClick;
}
private void VM_OnExitClick()
{
Handlers.GetHandler<ViewModels.Pages.MenuPage.ViewModel>()?.ShowView();
}
}
}
using ViewModels;
using GS.Terminal.LogicShell.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GS.Terminal.VisitorSelfService.Logic.Core
{
public abstract class Corebase
{
public abstract void Init();
private Locator _vmLocator;
protected Locator vmLocator
{
get
{
if (_vmLocator == null)
_vmLocator = Program.vmLocator;
return _vmLocator;
}
}
}
public abstract class Corebase<VMTYPE> : Corebase where VMTYPE : IViewModel
{
private VMTYPE _VM;
protected VMTYPE VM
{
get
{
if (_VM == null) _VM = vmLocator.GetViewModel<VMTYPE>();
return _VM;
}
}
/// <summary>
/// 打开当前页面
/// </summary>
public IPopWindowHandler Open()
{
return ThirdAddon.LogicShell.Open(VM);
}
public void ShowView()
{
ThirdAddon.LogicShell.ShowView(VM);
}
}
}
using GS.Terminal.VisitorSelfService.Logic.ThirdAddon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GS.Terminal.VisitorSelfService.Logic.Core
{
public class MenuPageHandler : Corebase<ViewModels.Pages.MenuPage.ViewModel>
{
public override void Init()
{
VM.OnMenuItemClick += VM_OnMenuItemClick;
}
private void VM_OnMenuItemClick(ViewModels.Pages.MenuPage.MenuItemType menuItem)
{
if (menuItem == ViewModels.Pages.MenuPage.MenuItemType.HasAppointment)
{
ThirdAddon.LogicShell.ShowView(vmLocator.AuthenticationPage);
}
else
{
}
}
}
}
......@@ -92,6 +92,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Core\AuthenticationPageHandler.cs" />
<Compile Include="Core\Corebase.cs" />
<Compile Include="Core\MenuPageHandler.cs" />
<Compile Include="Handlers.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ThirdAddon\DeviceManager.cs" />
......@@ -119,6 +123,7 @@
<Name>ViewModels</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
......
using GS.Terminal.LogicShell.Interface;
using GS.Terminal.VisitorSelfService.Logic.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace GS.Terminal.VisitorSelfService.Logic
{
public class Handlers
{
private static Dictionary<Type, object> handlers =
new Dictionary<Type, object>();
internal static void Product()
{
var current = Assembly.GetCallingAssembly();
var types = current.GetTypes();
var h = types.Where(ss => typeof(Corebase).IsAssignableFrom(ss) &&
!ss.Name.StartsWith("Corebase")).ToList();
h.ForEach(item =>
{
var handler = (Corebase)Activator.CreateInstance(item);
handler.Init();
handlers.Add(item.BaseType.GenericTypeArguments[0], handler);
});
}
/// <summary>
/// 根据viewmodel获取handler
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
internal static Corebase<T> GetHandler<T>() where T : IViewModel
{
if (handlers.TryGetValue(typeof(T), out object instance))
return (Corebase<T>)instance;
return null;
}
}
}
......@@ -12,19 +12,20 @@ namespace GS.Terminal.VisitorSelfService.Logic
public class Program : IAddonActivator
{
internal static IAddonContext _Context;
internal static Locator _Locator;
internal static Locator vmLocator;
public void Start(IAddonContext Context)
{
_Context = Context;
_Locator = new Locator();
vmLocator = new Locator();
AddonRuntime.Instance.onRuntimeCompleted += Instance_onRuntimeCompleted;
}
private void Instance_onRuntimeCompleted(object sender)
{
ThirdAddon.LogicShell.ShowView(_Locator.HistoryMenuPage);
Handlers.Product();
ThirdAddon.LogicShell.ShowView(vmLocator.MenuPage);
}
public void Stop(IAddonContext Context)
......
......@@ -32,6 +32,11 @@ namespace ViewModels
SimpleIoc.Default.Register<T>();
}
public T GetViewModel<T>()
{
return SimpleIoc.Default.GetInstance<T>();
}
public Foot Foot
{
get
......
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GS.Terminal.LogicShell.Interface;
using System;
using System.Collections.Generic;
......@@ -12,6 +13,12 @@ namespace ViewModels.Pages.AuthenticationPage
{
public string ViewID => "bbf0b764-4029-4db8-8beb-97b9627a4ac5";
public event Action OnExitClick;
public RelayCommand ExitCommand => new RelayCommand(() =>
{
OnExitClick?.Invoke();
});
public void Reset()
{
......
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GS.Terminal.LogicShell.Interface;
using System;
using System.Collections.Generic;
......@@ -8,10 +9,36 @@ using System.Threading.Tasks;
namespace ViewModels.Pages.MenuPage
{
public delegate void OnMenuItemClickDelegate(MenuItemType menuItem);
public enum MenuItemType
{
HasAppointment,
NoAppointment
}
public partial class ViewModel : ViewModelBase, IViewModel
{
public string ViewID => "31fa8506-2213-44a4-bd07-a68fba474038";
private int _TodayPerson;
public int TodayPerson
{
get { return _TodayPerson; }
set { _TodayPerson = value; RaisePropertyChanged("TodayPerson"); }
}
public event OnMenuItemClickDelegate OnMenuItemClick;
public RelayCommand HasAppointmentCommand => new RelayCommand(() =>
{
OnMenuItemClick?.Invoke(MenuItemType.HasAppointment);
});
public RelayCommand NoAppointmentCommand => new RelayCommand(() =>
{
OnMenuItemClick?.Invoke(MenuItemType.NoAppointment);
});
public void Reset()
{
......
......@@ -5,10 +5,10 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
d:DesignHeight="45" d:DesignWidth="1080"
Title="Foot">
<Grid>
<TextBlock Text="设备编号: SYL106" Foreground="White" HorizontalAlignment="Right" VerticalAlignment="Center" FontSize="24"/>
<TextBlock Text="青岛通软网络科技有限公司" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="24"/>
</Grid>
</Page>
......@@ -12,6 +12,7 @@
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style.xaml"/>
<ResourceDictionary Source="../../Resources/CommonDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
......@@ -72,6 +73,12 @@
</Grid>
</Grid>
<Button Grid.Row="2" Template="{StaticResource CommonButton}"
Command="{Binding ExitCommand}"
Width="250" Height="60" Content="退出" Foreground="#2a2b2d" FontSize="24">
</Button>
</Grid>
</Grid>
</Page>
......@@ -26,14 +26,14 @@
</Grid.RowDefinitions>
<StackPanel>
<TextBlock HorizontalAlignment="Center">
<Run Text="290" FontSize="80"/>
<Run Text="{Binding TodayPerson}" FontSize="80"/>
<Run Text="人" FontSize="14"/>
</TextBlock>
<TextBlock Text="今日本机已到访" FontSize="40" HorizontalAlignment="Center"/>
</StackPanel>
<gs:ImageButton Width="500" Height="500" Grid.Row="1" ImgPath="/Views;component/Resources/menu_btn_has.png"/>
<gs:ImageButtonFix Command="{Binding HasAppointmentCommand}" Width="500" Height="500" Grid.Row="1" Image="/Views;component/Resources/menu_btn_has.png"/>
<Image Grid.Row="2" Source="/Views;component/Resources/menu_or.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<gs:ImageButton Width="500" Height="500" Grid.Row="3" ImgPath="/Views;component/Resources/menu_btn_no.png"/>
<gs:ImageButtonFix Command="{Binding NoAppointmentCommand}" Width="500" Height="500" Grid.Row="3" Image="/Views;component/Resources/menu_btn_no.png"/>
<Button Style="{StaticResource MenuPage_HistoryButton}"/>
</Grid>
</Grid>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment