当前位置:   article > 正文

wpf prism 数据绑定_prism binding wpf

prism binding wpf

1 新建wpf应用程序

2 添加prism.unity(v8.0)的引用

3 新建Views文件夹讲MainWindow拖到该文件夹中,修改MainWindow.xaml和MainWindow.xaml.cs 注意要修改相关命名空间(3处)

  1. <Window x:Class="PrismBindingDemo.Views.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:PrismBindingDemo.Views"
  7. xmlns:prism="http://prismlibrary.com/"
  8. mc:Ignorable="d"
  9. Title="MainWindow" Height="450" Width="800" prism:ViewModelLocator.AutoWireViewModel="True">
  10. <StackPanel>
  11. <TextBox Text="{Binding Text}" Margin="10" Height="100" FontSize="50" Foreground="Black" BorderBrush="Black"/>
  12. <Button Height="100" Width="300" Content="Click Me" FontSize="50" Command="{Binding ClickCommnd}"/>
  13. </StackPanel>
  14. </Window>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace PrismBindingDemo.Views
  16. {
  17. /// <summary>
  18. /// MainWindow.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class MainWindow : Window
  21. {
  22. public MainWindow()
  23. {
  24. InitializeComponent();
  25. }
  26. }
  27. }

4 添加ViewModels文件夹 ,添加类MainWindowViewModel

  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace PrismBindingDemo.ViewModels
  9. {
  10. public class MainWindowViewModel: BindableBase
  11. {
  12. private string _text;
  13. public string Text
  14. {
  15. get { return _text; }
  16. set { SetProperty(ref _text, value); }
  17. }
  18. private DelegateCommand _clickCommnd;
  19. public DelegateCommand ClickCommnd =>
  20. _clickCommnd ?? (_clickCommnd = new DelegateCommand(ExecuteClickCommnd));
  21. void ExecuteClickCommnd()
  22. {
  23. this.Text = "Click Me!";
  24. }
  25. public MainWindowViewModel()
  26. {
  27. this.Text = "Hello Prism!";
  28. }
  29. }
  30. }

5 修改App.xaml和App.xaml.cs

  1. <prism:PrismApplication x:Class="PrismBindingDemo.App"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:local="clr-namespace:PrismBindingDemo"
  5. xmlns:prism="http://prismlibrary.com/"
  6. >
  7. <Application.Resources>
  8. </Application.Resources>
  9. </prism:PrismApplication>
  1. using Prism.Unity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using Prism.Ioc;
  10. using PrismBindingDemo.Views;
  11. namespace PrismBindingDemo
  12. {
  13. /// <summary>
  14. /// App.xaml 的交互逻辑
  15. /// </summary>
  16. public partial class App : PrismApplication
  17. {
  18. protected override Window CreateShell()
  19. {
  20. return Container.Resolve<MainWindow>();
  21. }
  22. protected override void RegisterTypes(IContainerRegistry containerRegistry)
  23. {
  24. }
  25. }
  26. }

6 运行

 

7 以上视图xxx.xaml放在Views目录下,对应的xxxViewModel.cs放在ViewModels目录下是默认设置,如果更改了以上目录则需要在App.xaml.cs中重写ConfigureViewModelLocator()

  1. protected override void ConfigureViewModelLocator()
  2. {
  3. base.ConfigureViewModelLocator();
  4. ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
  5. {
  6. var viewName = viewType.FullName.Replace(".Viewsb.", ".ViewModelsa.OhMyGod.");
  7. var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
  8. var viewModelName = $"{viewName}Test, {viewAssemblyName}";
  9. return Type.GetType(viewModelName);
  10. });
  11. }

8  自定义ViewModel注册

  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. namespace PrismSample
  4. {
  5. public class Foo:BindableBase
  6. {
  7. private string _text;
  8. public string Text
  9. {
  10. get { return _text; }
  11. set { SetProperty(ref _text, value); }
  12. }
  13. public Foo()
  14. {
  15. this.Text = "Foo";
  16. }
  17. private DelegateCommand _clickCommnd;
  18. public DelegateCommand ClickCommnd =>
  19. _clickCommnd ?? (_clickCommnd = new DelegateCommand(ExecuteClickCommnd));
  20. void ExecuteClickCommnd()
  21. {
  22. this.Text = "Oh My God!";
  23. }
  24. }
  25. }

App.xaml.cs中重写ConfigureViewModelLocator的方法 

  1. protected override void ConfigureViewModelLocator()
  2. {
  3. base.ConfigureViewModelLocator();
  4. ViewModelLocationProvider.Register<MainWindow, Foo>();
  5. //ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) =>
  6. //{
  7. // var viewName = viewType.FullName.Replace(".Viewsb.", ".ViewModelsa.OhMyGod.");
  8. // var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
  9. // var viewModelName = $"{viewName}Test, {viewAssemblyName}";
  10. // return Type.GetType(viewModelName);
  11. //});
  12. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/746041
推荐阅读
相关标签
  

闽ICP备14008679号