{"id":61719,"date":"2026-01-18T13:18:38","date_gmt":"2026-01-18T05:18:38","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/61719.html"},"modified":"2026-01-18T13:18:38","modified_gmt":"2026-01-18T05:18:38","slug":"prism%e7%bb%91%e5%ae%9a%e8%af%a6%e8%a7%a3%ef%bc%9a%e4%bb%8e%e5%9f%ba%e7%a1%80%e5%88%b0%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/61719.html","title":{"rendered":"Prism\u7ed1\u5b9a\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u5b9e\u6218"},"content":{"rendered":"<h2>\u4e00\u3001Prism\u7ed1\u5b9a\u662f\u4ec0\u4e48&#xff1f;<\/h2>\n<h4>1.1 \u6838\u5fc3\u6982\u5ff5<\/h4>\n<p>\u7ed1\u5b9a\u5c31\u50cf\u662f\u5728UI\u63a7\u4ef6\u548c\u6570\u636e\u6e90\u4e4b\u95f4\u5efa\u7acb\u4e00\u6761&#034;\u53cc\u5411\u901a\u4fe1\u7ba1\u9053&#034;\u3002\u60f3\u8c61\u4e00\u4e0b&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5355\u5411\u7ed1\u5b9a&#xff1a;\u5c31\u50cf\u5929\u6c14\u9884\u62a5\u663e\u793a&#xff0c;\u6570\u636e\u6e90\u53d8\u4e86&#xff0c;UI\u81ea\u52a8\u66f4\u65b0<\/p>\n<\/li>\n<li>\n<p>\u53cc\u5411\u7ed1\u5b9a&#xff1a;\u5c31\u50cf\u6e29\u5ea6\u8c03\u8282\u5668&#xff0c;UI\u53ef\u4ee5\u4fee\u6539\u6570\u636e&#xff0c;\u6570\u636e\u53d8\u5316\u4e5f\u4f1a\u66f4\u65b0UI<\/p>\n<\/li>\n<\/ul>\n<p>\u5728Prism\u4e2d&#xff0c;\u6211\u4eec\u901a\u5e38\u4f7f\u7528MVVM\u6a21\u5f0f&#xff0c;\u7ed1\u5b9a\u8fde\u63a5\u7684\u662f&#xff1a;<\/p>\n<ul>\n<li>\n<p>View&#xff1a;\u7528\u6237\u754c\u9762&#xff08;XAML\u6587\u4ef6&#xff09;<\/p>\n<\/li>\n<li>\n<p>ViewModel&#xff1a;\u4e1a\u52a1\u903b\u8f91\u548c\u6570\u636e&#xff08;C#\u7c7b&#xff09;<\/p>\n<\/li>\n<li>\n<p>Model&#xff1a;\u6570\u636e\u6a21\u578b<\/p>\n<\/li>\n<\/ul>\n<h4>1.2 \u7ed1\u5b9a\u5728Prism\u4e2d\u7684\u7279\u6b8a\u4f18\u52bf<\/h4>\n<p>Prism\u589e\u5f3a\u4e86WPF\/Xamarin\u7684\u9ed8\u8ba4\u7ed1\u5b9a\u529f\u80fd&#xff0c;\u63d0\u4f9b&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5<\/p>\n<\/li>\n<li>\n<p>\u66f4\u597d\u7684\u89e3\u8026<\/p>\n<\/li>\n<li>\n<p>\u5185\u7f6e\u7684\u547d\u4ee4\u7ed1\u5b9a\u652f\u6301<\/p>\n<\/li>\n<li>\n<p>\u533a\u57df\u5bfc\u822a\u65f6\u7684\u6570\u636e\u4fdd\u6301<\/p>\n<\/li>\n<\/ul>\n<h3>\u4e8c\u3001\u7ed1\u5b9a\u7684\u6838\u5fc3\u7c7b\u578b\u548c\u793a\u4f8b<\/h3>\n<h4>2.1 \u57fa\u672c\u6570\u636e\u7ed1\u5b9a<\/h4>\n<p>csharp<\/p>\n<p>\/\/ ViewModel<br \/>\npublic class PersonViewModel : BindableBase<br \/>\n{<br \/>\n    private string _name;<br \/>\n    public string Name<br \/>\n    {<br \/>\n        get { return _name; }<br \/>\n        set { SetProperty(ref _name, value); }<br \/>\n    }<\/p>\n<p>    private int _age;<br \/>\n    public int Age<br \/>\n    {<br \/>\n        get { return _age; }<br \/>\n        set { SetProperty(ref _age, value); }<br \/>\n    }<\/p>\n<p>    \/\/ \u53ea\u8bfb\u5c5e\u6027&#xff08;\u8ba1\u7b97\u5c5e\u6027&#xff09;<br \/>\n    public string Description &#061;&gt; $&#034;{Name}, {Age}\u5c81&#034;;<\/p>\n<p>    \/\/ \u96c6\u5408<br \/>\n    private ObservableCollection&lt;string&gt; _hobbies &#061; new ObservableCollection&lt;string&gt;();<br \/>\n    public ObservableCollection&lt;string&gt; Hobbies<br \/>\n    {<br \/>\n        get { return _hobbies; }<br \/>\n        set { SetProperty(ref _hobbies, value); }<br \/>\n    }<br \/>\n}<\/p>\n<p>xml<\/p>\n<p>&lt;!&#8211; View &#8211;&gt;<br \/>\n&lt;StackPanel&gt;<br \/>\n    &lt;!&#8211; \u53cc\u5411\u7ed1\u5b9a &#8211;&gt;<br \/>\n    &lt;TextBox Text&#061;&#034;{Binding Name, Mode&#061;TwoWay, UpdateSourceTrigger&#061;PropertyChanged}&#034;<br \/>\n             Margin&#061;&#034;5&#034;\/&gt;<\/p>\n<p>    &lt;!&#8211; \u5355\u5411\u7ed1\u5b9a&#xff08;\u53ea\u663e\u793a&#xff0c;\u4e0d\u80fd\u7f16\u8f91&#xff09; &#8211;&gt;<br \/>\n    &lt;TextBlock Text&#061;&#034;{Binding Description}&#034;<br \/>\n               Margin&#061;&#034;5&#034;\/&gt;<\/p>\n<p>    &lt;!&#8211; \u5217\u8868\u7ed1\u5b9a &#8211;&gt;<br \/>\n    &lt;ListBox ItemsSource&#061;&#034;{Binding Hobbies}&#034;<br \/>\n             Margin&#061;&#034;5&#034;\/&gt;<br \/>\n&lt;\/StackPanel&gt;<\/p>\n<h4>2.2 \u547d\u4ee4\u7ed1\u5b9a&#xff08;\u6700\u5e38\u7528\u7684Prism\u7279\u6027&#xff09;<\/h4>\n<p>csharp<\/p>\n<p>\/\/ ViewModel<br \/>\npublic class MainViewModel : BindableBase<br \/>\n{<br \/>\n    \/\/ \u4f7f\u7528Prism\u7684DelegateCommand<br \/>\n    private DelegateCommand _saveCommand;<br \/>\n    public DelegateCommand SaveCommand &#061;&gt;<br \/>\n        _saveCommand ?? (_saveCommand &#061; new DelegateCommand(ExecuteSave, CanSave));<\/p>\n<p>    private void ExecuteSave()<br \/>\n    {<br \/>\n        \/\/ \u4fdd\u5b58\u903b\u8f91<br \/>\n        MessageBox.Show(&#034;\u4fdd\u5b58\u6210\u529f&#xff01;&#034;);<br \/>\n    }<\/p>\n<p>    private bool CanSave()<br \/>\n    {<br \/>\n        \/\/ \u53ea\u6709\u59d3\u540d\u4e0d\u4e3a\u7a7a\u65f6\u624d\u542f\u7528\u4fdd\u5b58\u6309\u94ae<br \/>\n        return !string.IsNullOrEmpty(Name);<br \/>\n    }<\/p>\n<p>    \/\/ \u5e26\u53c2\u6570\u7684\u547d\u4ee4<br \/>\n    private DelegateCommand&lt;string&gt; _deleteCommand;<br \/>\n    public DelegateCommand&lt;string&gt; DeleteCommand &#061;&gt;<br \/>\n        _deleteCommand ?? (_deleteCommand &#061; new DelegateCommand&lt;string&gt;(ExecuteDelete));<\/p>\n<p>    private void ExecuteDelete(string itemId)<br \/>\n    {<br \/>\n        \/\/ \u5220\u9664\u6307\u5b9a\u9879\u76ee<br \/>\n    }<br \/>\n}<\/p>\n<p>xml<\/p>\n<p>&lt;!&#8211; View &#8211;&gt;<br \/>\n&lt;StackPanel&gt;<br \/>\n    &lt;Button Content&#061;&#034;\u4fdd\u5b58&#034;<br \/>\n            Command&#061;&#034;{Binding SaveCommand}&#034;<br \/>\n            Margin&#061;&#034;5&#034;\/&gt;<\/p>\n<p>    &lt;!&#8211; \u5e26\u53c2\u6570\u7684\u547d\u4ee4\u7ed1\u5b9a &#8211;&gt;<br \/>\n    &lt;Button Content&#061;&#034;\u5220\u9664\u9879\u76ee1&#034;<br \/>\n            Command&#061;&#034;{Binding DeleteCommand}&#034;<br \/>\n            CommandParameter&#061;&#034;item1&#034;<br \/>\n            Margin&#061;&#034;5&#034;\/&gt;<br \/>\n&lt;\/StackPanel&gt;<\/p>\n<h4>2.3 \u4e8b\u4ef6\u7ed1\u5b9a&#xff08;EventToCommand&#xff09;<\/h4>\n<p>xml<\/p>\n<p>&lt;!&#8211; \u9700\u8981\u5f15\u7528Prism.Wpf &#8211;&gt;<br \/>\n&lt;Button Content&#061;&#034;\u70b9\u51fb\u6211&#034; Margin&#061;&#034;5&#034;&gt;<br \/>\n    &lt;i:Interaction.Triggers&gt;<br \/>\n        &lt;i:EventTrigger EventName&#061;&#034;Click&#034;&gt;<br \/>\n            &lt;prism:InvokeCommandAction Command&#061;&#034;{Binding ButtonClickCommand}&#034;\/&gt;<br \/>\n        &lt;\/i:EventTrigger&gt;<br \/>\n        &lt;i:EventTrigger EventName&#061;&#034;MouseEnter&#034;&gt;<br \/>\n            &lt;prism:InvokeCommandAction<br \/>\n                Command&#061;&#034;{Binding MouseEnterCommand}&#034;<br \/>\n                CommandParameter&#061;&#034;{Binding ElementName&#061;myButton, Path&#061;Content}&#034;\/&gt;<br \/>\n        &lt;\/i:EventTrigger&gt;<br \/>\n    &lt;\/i:Interaction.Triggers&gt;<br \/>\n&lt;\/Button&gt;<\/p>\n<h4>2.4 \u503c\u8f6c\u6362\u5668&#xff08;Converter&#xff09;<\/h4>\n<p>csharp<\/p>\n<p>\/\/ \u5e03\u5c14\u503c\u8f6c\u6362\u5668&#xff1a;\u5c06bool\u8f6c\u6362\u4e3aVisibility<br \/>\npublic class BoolToVisibilityConverter : IValueConverter<br \/>\n{<br \/>\n    public object Convert(object value, Type targetType,<br \/>\n        object parameter, CultureInfo culture)<br \/>\n    {<br \/>\n        return (value is bool boolValue &amp;&amp; boolValue)<br \/>\n            ? Visibility.Visible<br \/>\n            : Visibility.Collapsed;<br \/>\n    }<\/p>\n<p>    public object ConvertBack(object value, Type targetType,<br \/>\n        object parameter, CultureInfo culture)<br \/>\n    {<br \/>\n        return value is Visibility visibility &amp;&amp;<br \/>\n               visibility &#061;&#061; Visibility.Visible;<br \/>\n    }<br \/>\n}<\/p>\n<p>xml<\/p>\n<p>&lt;!&#8211; \u5728XAML\u4e2d\u4f7f\u7528\u8f6c\u6362\u5668 &#8211;&gt;<br \/>\n&lt;UserControl.Resources&gt;<br \/>\n    &lt;local:BoolToVisibilityConverter x:Key&#061;&#034;BoolToVisibility&#034;\/&gt;<br \/>\n&lt;\/UserControl.Resources&gt;<\/p>\n<p>&lt;StackPanel&gt;<br \/>\n    &lt;!&#8211; \u5f53IsAdmin\u4e3atrue\u65f6\u663e\u793a\u7ba1\u7406\u9762\u677f &#8211;&gt;<br \/>\n    &lt;StackPanel Visibility&#061;&#034;{Binding IsAdmin,<br \/>\n        Converter&#061;{StaticResource BoolToVisibility}}&#034;&gt;<br \/>\n        &lt;Button Content&#061;&#034;\u7ba1\u7406\u529f\u80fd1&#034;\/&gt;<br \/>\n        &lt;Button Content&#061;&#034;\u7ba1\u7406\u529f\u80fd2&#034;\/&gt;<br \/>\n    &lt;\/StackPanel&gt;<br \/>\n&lt;\/StackPanel&gt;<\/p>\n<h3>\u4e09\u3001\u6700\u4f73\u5b9e\u8df5<\/h3>\n<h4>3.1 \u6570\u636e\u7ed1\u5b9a\u6700\u4f73\u5b9e\u8df5<\/h4>\n<li>\n<p>\u59cb\u7ec8\u4f7f\u7528BindableBase<\/p>\n<p>csharp<\/p>\n<p>\/\/ \u6b63\u786e\u505a\u6cd5<br \/>\npublic class MyViewModel : BindableBase<br \/>\n{<br \/>\n    private string _title;<br \/>\n    public string Title<br \/>\n    {<br \/>\n        get &#061;&gt; _title;<br \/>\n        set &#061;&gt; SetProperty(ref _title, value);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u9519\u8bef\u505a\u6cd5&#xff08;\u4e0d\u4f1a\u901a\u77e5UI\u66f4\u65b0&#xff09;<br \/>\npublic class BadViewModel<br \/>\n{<br \/>\n    public string Title { get; set; }  \/\/ UI\u4e0d\u4f1a\u81ea\u52a8\u66f4\u65b0&#xff01;<br \/>\n}\n<\/li>\n<li>\n<p>\u96c6\u5408\u4f7f\u7528ObservableCollection&lt;T&gt;<\/p>\n<p>csharp<\/p>\n<p>\/\/ \u6b63\u786e&#xff1a;\u96c6\u5408\u53d8\u5316\u4f1a\u81ea\u52a8\u901a\u77e5UI<br \/>\npublic ObservableCollection&lt;Person&gt; People { get; }<br \/>\n    &#061; new ObservableCollection&lt;Person&gt;();<\/p>\n<p>\/\/ \u6dfb\u52a0\/\u5220\u9664\u9879\u76ee\u4f1a\u81ea\u52a8\u66f4\u65b0UI<br \/>\nPeople.Add(new Person());<br \/>\nPeople.RemoveAt(0);\n<\/li>\n<li>\n<p>\u5408\u7406\u4f7f\u7528\u7ed1\u5b9a\u6a21\u5f0f<\/p>\n<p>xml<\/p>\n<p>&lt;!&#8211; \u6839\u636e\u573a\u666f\u9009\u62e9\u7ed1\u5b9a\u6a21\u5f0f &#8211;&gt;<br \/>\n&lt;!&#8211; \u8f93\u5165\u63a7\u4ef6\u7528TwoWay &#8211;&gt;<br \/>\n&lt;TextBox Text&#061;&#034;{Binding UserName, Mode&#061;TwoWay,<br \/>\n         UpdateSourceTrigger&#061;PropertyChanged}&#034;\/&gt;<\/p>\n<p>&lt;!&#8211; \u53ea\u663e\u793a\u7684\u5185\u5bb9\u7528OneWay &#8211;&gt;<br \/>\n&lt;TextBlock Text&#061;&#034;{Binding CurrentTime, Mode&#061;OneWay}&#034;\/&gt;<\/p>\n<p>&lt;!&#8211; \u521d\u59cb\u5316\u540e\u4e0d\u518d\u53d8\u5316\u7684\u7528OneTime &#8211;&gt;<br \/>\n&lt;TextBlock Text&#061;&#034;{Binding AppVersion, Mode&#061;OneTime}&#034;\/&gt;\n<\/li>\n<h4>3.2 \u547d\u4ee4\u7ed1\u5b9a\u6700\u4f73\u5b9e\u8df5<\/h4>\n<li>\n<p>\u4f7f\u7528DelegateCommand\u7684\u5ef6\u8fdf\u521d\u59cb\u5316<\/p>\n<p>csharp<\/p>\n<p>private DelegateCommand _refreshCommand;<br \/>\npublic DelegateCommand RefreshCommand &#061;&gt;<br \/>\n    _refreshCommand ?? (_refreshCommand &#061; new DelegateCommand(<br \/>\n        () &#061;&gt; LoadData(),<br \/>\n        () &#061;&gt; !IsLoading));\n<\/li>\n<li>\n<p>\u547d\u4ee4\u542f\u7528\u72b6\u6001\u7ba1\u7406<\/p>\n<p>csharp<\/p>\n<p>public class OrderViewModel : BindableBase<br \/>\n{<br \/>\n    private bool _canCheckout;<br \/>\n    public bool CanCheckout<br \/>\n    {<br \/>\n        get &#061;&gt; _canCheckout;<br \/>\n        set<br \/>\n        {<br \/>\n            SetProperty(ref _canCheckout, value);<br \/>\n            \/\/ \u66f4\u65b0\u547d\u4ee4\u7684\u542f\u7528\u72b6\u6001<br \/>\n            CheckoutCommand.RaiseCanExecuteChanged();<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public DelegateCommand CheckoutCommand { get; }<\/p>\n<p>    public OrderViewModel()<br \/>\n    {<br \/>\n        CheckoutCommand &#061; new DelegateCommand(<br \/>\n            Checkout,<br \/>\n            () &#061;&gt; CanCheckout);<br \/>\n    }<br \/>\n}\n<\/li>\n<h4>3.3 \u6027\u80fd\u4f18\u5316<\/h4>\n<li>\n<p>\u865a\u62df\u5316\u5927\u6570\u636e\u96c6\u5408<\/p>\n<p>xml<\/p>\n<p>&lt;!&#8211; \u5bf9\u4e8e\u5927\u91cf\u6570\u636e\u7684\u5217\u8868&#xff0c;\u542f\u7528\u865a\u62df\u5316 &#8211;&gt;<br \/>\n&lt;ListBox VirtualizingStackPanel.IsVirtualizing&#061;&#034;True&#034;<br \/>\n         VirtualizingStackPanel.VirtualizationMode&#061;&#034;Recycling&#034;&gt;<br \/>\n    &lt;!&#8211; &#8230; &#8211;&gt;<br \/>\n&lt;\/ListBox&gt;\n<\/li>\n<li>\n<p>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u7ed1\u5b9a\u66f4\u65b0<\/p>\n<p>xml<\/p>\n<p>&lt;!&#8211; \u4e0d\u662f\u6240\u6709\u7ed1\u5b9a\u90fd\u9700\u8981PropertyChanged\u66f4\u65b0 &#8211;&gt;<br \/>\n&lt;TextBox Text&#061;&#034;{Binding SearchText, Mode&#061;TwoWay,<br \/>\n         UpdateSourceTrigger&#061;LostFocus}&#034;\/&gt;  &lt;!&#8211; \u5931\u53bb\u7126\u70b9\u65f6\u66f4\u65b0 &#8211;&gt;\n<\/li>\n<h3>\u56db\u3001\u6700\u4f73\u4f7f\u7528\u573a\u666f<\/h3>\n<h4>4.1 \u8868\u5355\u5e94\u7528<\/h4>\n<p>csharp<\/p>\n<p>\/\/ ViewModel<br \/>\npublic class RegistrationViewModel : BindableBase<br \/>\n{<br \/>\n    \/\/ \u8868\u5355\u5b57\u6bb5<br \/>\n    public string Email { get; set; }<br \/>\n    public string Password { get; set; }<\/p>\n<p>    \/\/ \u8868\u5355\u9a8c\u8bc1<br \/>\n    public bool IsValid &#061;&gt; !string.IsNullOrEmpty(Email) &amp;&amp;<br \/>\n                          !string.IsNullOrEmpty(Password);<\/p>\n<p>    \/\/ \u63d0\u4ea4\u547d\u4ee4<br \/>\n    public DelegateCommand SubmitCommand { get; }<\/p>\n<p>    public RegistrationViewModel()<br \/>\n    {<br \/>\n        SubmitCommand &#061; new DelegateCommand(<br \/>\n            Submit,<br \/>\n            () &#061;&gt; IsValid);<\/p>\n<p>        \/\/ \u5f53\u5c5e\u6027\u53d8\u5316\u65f6\u91cd\u65b0\u9a8c\u8bc1<br \/>\n        PropertyChanged &#043;&#061; (s, e) &#061;&gt;<br \/>\n            SubmitCommand.RaiseCanExecuteChanged();<br \/>\n    }<br \/>\n}<\/p>\n<h4>4.2 \u4e3b\u4ece\u89c6\u56fe<\/h4>\n<p>xml<\/p>\n<p>&lt;!&#8211; \u4e3b\u5217\u8868 &#8211;&gt;<br \/>\n&lt;ListBox x:Name&#061;&#034;MasterList&#034;<br \/>\n         ItemsSource&#061;&#034;{Binding Customers}&#034;<br \/>\n         SelectedItem&#061;&#034;{Binding SelectedCustomer, Mode&#061;TwoWay}&#034;&gt;<br \/>\n    &lt;ListBox.ItemTemplate&gt;<br \/>\n        &lt;DataTemplate&gt;<br \/>\n            &lt;TextBlock Text&#061;&#034;{Binding Name}&#034;\/&gt;<br \/>\n        &lt;\/DataTemplate&gt;<br \/>\n    &lt;\/ListBox.ItemTemplate&gt;<br \/>\n&lt;\/ListBox&gt;<\/p>\n<p>&lt;!&#8211; \u8be6\u7ec6\u4fe1\u606f&#xff08;\u4ece\u89c6\u56fe&#xff09; &#8211;&gt;<br \/>\n&lt;ContentControl Content&#061;&#034;{Binding SelectedCustomer}&#034;&gt;<br \/>\n    &lt;ContentControl.ContentTemplate&gt;<br \/>\n        &lt;DataTemplate&gt;<br \/>\n            &lt;StackPanel&gt;<br \/>\n                &lt;TextBox Text&#061;&#034;{Binding Name, Mode&#061;TwoWay}&#034;\/&gt;<br \/>\n                &lt;TextBox Text&#061;&#034;{Binding Email, Mode&#061;TwoWay}&#034;\/&gt;<br \/>\n            &lt;\/StackPanel&gt;<br \/>\n        &lt;\/DataTemplate&gt;<br \/>\n    &lt;\/ContentControl.ContentTemplate&gt;<br \/>\n&lt;\/ContentControl&gt;<\/p>\n<h4>4.3 \u52a8\u6001UI<\/h4>\n<p>csharp<\/p>\n<p>\/\/ \u6839\u636e\u7528\u6237\u6743\u9650\u52a8\u6001\u663e\u793a\u4e0d\u540cUI<br \/>\npublic class DashboardViewModel : BindableBase<br \/>\n{<br \/>\n    private UserRole _currentRole;<br \/>\n    public UserRole CurrentRole<br \/>\n    {<br \/>\n        get &#061;&gt; _currentRole;<br \/>\n        set &#061;&gt; SetProperty(ref _currentRole, value);<br \/>\n    }<\/p>\n<p>    \/\/ \u6839\u636e\u89d2\u8272\u51b3\u5b9a\u663e\u793a\u54ea\u4e9b\u6a21\u5757<br \/>\n    public bool ShowAdminPanel &#061;&gt; CurrentRole &#061;&#061; UserRole.Admin;<br \/>\n    public bool ShowUserPanel &#061;&gt; CurrentRole &gt;&#061; UserRole.User;<br \/>\n    public bool ShowGuestPanel &#061;&gt; CurrentRole &gt;&#061; UserRole.Guest;<br \/>\n}<\/p>\n<h3>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<h4>5.1 \u7ed1\u5b9a\u4e0d\u5de5\u4f5c<\/h4>\n<li>\n<p>\u68c0\u67e5DataContext\u662f\u5426\u6b63\u786e\u8bbe\u7f6e<\/p>\n<\/li>\n<li>\n<p>\u68c0\u67e5\u5c5e\u6027\u540d\u79f0\u62fc\u5199<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u8c03\u8bd5\u8f93\u51fa<\/p>\n<p>xml<\/p>\n<p>&lt;!&#8211; \u5728XAML\u4e2d\u6dfb\u52a0&#xff0c;\u8c03\u8bd5\u7a97\u53e3\u4f1a\u663e\u793a\u7ed1\u5b9a\u9519\u8bef &#8211;&gt;<br \/>\n&lt;TextBlock Text&#061;&#034;{Binding SomeProperty,<br \/>\n             diag:PresentationTraceSources.TraceLevel&#061;High}&#034;\/&gt;\n<\/li>\n<h4>5.2 \u5185\u5b58\u6cc4\u6f0f<\/h4>\n<p>csharp<\/p>\n<p>\/\/ \u6b63\u786e\u6e05\u7406\u4e8b\u4ef6<br \/>\npublic class MyViewModel : IDisposable<br \/>\n{<br \/>\n    private CompositeDisposable _disposables &#061; new CompositeDisposable();<\/p>\n<p>    public MyViewModel()<br \/>\n    {<br \/>\n        \/\/ \u4f7f\u7528Prism\u7684EventAggregator\u65f6\u8981\u8bb0\u5f97\u53d6\u6d88\u8ba2\u9605<br \/>\n        var subscription &#061; _eventAggregator<br \/>\n            .GetEvent&lt;MyEvent&gt;()<br \/>\n            .Subscribe(OnEvent);<\/p>\n<p>        _disposables.Add(subscription);<br \/>\n    }<\/p>\n<p>    public void Dispose()<br \/>\n    {<br \/>\n        _disposables.Dispose();<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<p>Prism\u7ed1\u5b9a\u7684\u6838\u5fc3\u4f18\u52bf\u5728\u4e8e&#xff1a;<\/p>\n<li>\n<p>\u58f0\u660e\u5f0f\u7f16\u7a0b&#xff1a;\u5728XAML\u4e2d\u58f0\u660e\u7ed1\u5b9a\u5173\u7cfb&#xff0c;\u51cf\u5c11\u4ee3\u7801\u8026\u5408<\/p>\n<\/li>\n<li>\n<p>\u53cc\u5411\u6570\u636e\u6d41&#xff1a;\u81ea\u52a8\u540c\u6b65View\u548cViewModel\u7684\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u547d\u4ee4\u6a21\u5f0f&#xff1a;\u5c06\u7528\u6237\u64cd\u4f5c\u5c01\u88c5\u4e3a\u547d\u4ee4&#xff0c;\u4fbf\u4e8e\u6d4b\u8bd5\u548c\u7ef4\u62a4<\/p>\n<\/li>\n<li>\n<p>\u5f3a\u5927\u7684\u6269\u5c55\u6027&#xff1a;\u652f\u6301\u8f6c\u6362\u5668\u3001\u9a8c\u8bc1\u3001\u89e6\u53d1\u5668\u7b49<\/p>\n<\/li>\n<h4>\u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h4>\n<p>csharp<\/p>\n<p>\/\/ \u5b8c\u6574\u7684ViewModel\u793a\u4f8b<br \/>\npublic class ProductViewModel : BindableBase<br \/>\n{<br \/>\n    private Product _selectedProduct;<br \/>\n    public Product SelectedProduct<br \/>\n    {<br \/>\n        get &#061;&gt; _selectedProduct;<br \/>\n        set &#061;&gt; SetProperty(ref _selectedProduct, value);<br \/>\n    }<\/p>\n<p>    private ObservableCollection&lt;Product&gt; _products;<br \/>\n    public ObservableCollection&lt;Product&gt; Products<br \/>\n    {<br \/>\n        get &#061;&gt; _products;<br \/>\n        set &#061;&gt; SetProperty(ref _products, value);<br \/>\n    }<\/p>\n<p>    \/\/ \u547d\u4ee4<br \/>\n    public DelegateCommand AddProductCommand { get; }<br \/>\n    public DelegateCommand&lt;Product&gt; DeleteProductCommand { get; }<\/p>\n<p>    public ProductViewModel()<br \/>\n    {<br \/>\n        Products &#061; new ObservableCollection&lt;Product&gt;();<\/p>\n<p>        AddProductCommand &#061; new DelegateCommand(AddProduct);<br \/>\n        DeleteProductCommand &#061; new DelegateCommand&lt;Product&gt;(DeleteProduct);<\/p>\n<p>        \/\/ \u521d\u59cb\u5316\u6570\u636e<br \/>\n        LoadProducts();<br \/>\n    }<\/p>\n<p>    private void AddProduct()<br \/>\n    {<br \/>\n        var newProduct &#061; new Product { Name &#061; &#034;\u65b0\u4ea7\u54c1&#034;, Price &#061; 0 };<br \/>\n        Products.Add(newProduct);<br \/>\n        SelectedProduct &#061; newProduct;<br \/>\n    }<\/p>\n<p>    private void DeleteProduct(Product product)<br \/>\n    {<br \/>\n        if (product !&#061; null)<br \/>\n        {<br \/>\n            Products.Remove(product);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>Prism\u7ed1\u5b9a\u7684\u5173\u952e\u5728\u4e8e\u4fdd\u6301ViewModel\u7684\u7eaf\u7cb9\u6027&#xff08;\u4e0d\u5f15\u7528\u4efb\u4f55UI\u76f8\u5173\u7684\u4e1c\u897f&#xff09;\u548c\u5145\u5206\u5229\u7528\u6570\u636e\u901a\u77e5\u673a\u5236\u3002\u8fd9\u6837\u53ef\u4ee5\u8ba9\u4f60\u7684\u5e94\u7528\u66f4\u52a0\u53ef\u7ef4\u62a4\u3001\u53ef\u6d4b\u8bd5\u548c\u53ef\u6269\u5c55\u3002<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001Prism\u7ed1\u5b9a\u662f\u4ec0\u4e48&#xff1f;1.1 \u6838\u5fc3\u6982\u5ff5\u7ed1\u5b9a\u5c31\u50cf\u662f\u5728UI\u63a7\u4ef6\u548c\u6570\u636e\u6e90\u4e4b\u95f4\u5efa\u7acb\u4e00\u6761\\&#8221;\u53cc\u5411\u901a\u4fe1\u7ba1\u9053\\&#8221;\u3002\u60f3\u8c61\u4e00\u4e0b&#xff1a;\u5355\u5411\u7ed1\u5b9a&#xff1a;\u5c31\u50cf\u5929\u6c14\u9884\u62a5\u663e\u793a&#xff0c;\u6570\u636e\u6e90\u53d8\u4e86&#xff0c;UI\u81ea\u52a8\u66f4\u65b0\u53cc\u5411\u7ed1\u5b9a&#xff1a;\u5c31\u50cf\u6e29\u5ea6\u8c03\u8282\u5668&#xff0c;UI\u53ef\u4ee5\u4fee\u6539\u6570\u636e&#xff0c;\u6570\u636e\u53d8\u5316\u4e5f\u4f1a\u66f4\u65b0UI\u5728Prism\u4e2d&#xff0c;\u6211\u4eec\u901a\u5e38\u4f7f\u7528MVVM\u6a21\u5f0f&#xff0c;\u7ed1\u5b9a\u8fde\u63a5\u7684\u662f&#xff1a;View&#xff1a;\u7528\u6237\u754c\u9762&amp;#xff0<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[6542,3707],"topic":[],"class_list":["post-61719","post","type-post","status-publish","format-standard","hentry","category-server","tag-prism","tag-3707"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Prism\u7ed1\u5b9a\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/61719.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Prism\u7ed1\u5b9a\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001Prism\u7ed1\u5b9a\u662f\u4ec0\u4e48&#xff1f;1.1 \u6838\u5fc3\u6982\u5ff5\u7ed1\u5b9a\u5c31\u50cf\u662f\u5728UI\u63a7\u4ef6\u548c\u6570\u636e\u6e90\u4e4b\u95f4\u5efa\u7acb\u4e00\u6761&quot;\u53cc\u5411\u901a\u4fe1\u7ba1\u9053&quot;\u3002\u60f3\u8c61\u4e00\u4e0b&#xff1a;\u5355\u5411\u7ed1\u5b9a&#xff1a;\u5c31\u50cf\u5929\u6c14\u9884\u62a5\u663e\u793a&#xff0c;\u6570\u636e\u6e90\u53d8\u4e86&#xff0c;UI\u81ea\u52a8\u66f4\u65b0\u53cc\u5411\u7ed1\u5b9a&#xff1a;\u5c31\u50cf\u6e29\u5ea6\u8c03\u8282\u5668&#xff0c;UI\u53ef\u4ee5\u4fee\u6539\u6570\u636e&#xff0c;\u6570\u636e\u53d8\u5316\u4e5f\u4f1a\u66f4\u65b0UI\u5728Prism\u4e2d&#xff0c;\u6211\u4eec\u901a\u5e38\u4f7f\u7528MVVM\u6a21\u5f0f&#xff0c;\u7ed1\u5b9a\u8fde\u63a5\u7684\u662f&#xff1a;View&#xff1a;\u7528\u6237\u754c\u9762&amp;#xff0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/61719.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-18T05:18:38+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/61719.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/61719.html\",\"name\":\"Prism\u7ed1\u5b9a\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-18T05:18:38+00:00\",\"dateModified\":\"2026-01-18T05:18:38+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/61719.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/61719.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/61719.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Prism\u7ed1\u5b9a\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u5b9e\u6218\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Prism\u7ed1\u5b9a\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/61719.html","og_locale":"zh_CN","og_type":"article","og_title":"Prism\u7ed1\u5b9a\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001Prism\u7ed1\u5b9a\u662f\u4ec0\u4e48&#xff1f;1.1 \u6838\u5fc3\u6982\u5ff5\u7ed1\u5b9a\u5c31\u50cf\u662f\u5728UI\u63a7\u4ef6\u548c\u6570\u636e\u6e90\u4e4b\u95f4\u5efa\u7acb\u4e00\u6761\"\u53cc\u5411\u901a\u4fe1\u7ba1\u9053\"\u3002\u60f3\u8c61\u4e00\u4e0b&#xff1a;\u5355\u5411\u7ed1\u5b9a&#xff1a;\u5c31\u50cf\u5929\u6c14\u9884\u62a5\u663e\u793a&#xff0c;\u6570\u636e\u6e90\u53d8\u4e86&#xff0c;UI\u81ea\u52a8\u66f4\u65b0\u53cc\u5411\u7ed1\u5b9a&#xff1a;\u5c31\u50cf\u6e29\u5ea6\u8c03\u8282\u5668&#xff0c;UI\u53ef\u4ee5\u4fee\u6539\u6570\u636e&#xff0c;\u6570\u636e\u53d8\u5316\u4e5f\u4f1a\u66f4\u65b0UI\u5728Prism\u4e2d&#xff0c;\u6211\u4eec\u901a\u5e38\u4f7f\u7528MVVM\u6a21\u5f0f&#xff0c;\u7ed1\u5b9a\u8fde\u63a5\u7684\u662f&#xff1a;View&#xff1a;\u7528\u6237\u754c\u9762&amp;#xff0","og_url":"https:\/\/www.wsisp.com\/helps\/61719.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-18T05:18:38+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/61719.html","url":"https:\/\/www.wsisp.com\/helps\/61719.html","name":"Prism\u7ed1\u5b9a\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-18T05:18:38+00:00","dateModified":"2026-01-18T05:18:38+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/61719.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/61719.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/61719.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Prism\u7ed1\u5b9a\u8be6\u89e3\uff1a\u4ece\u57fa\u7840\u5230\u5b9e\u6218"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/61719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=61719"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/61719\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=61719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=61719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=61719"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=61719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}