English 中文(简体)
CommunityToolkit.MVVM 发出信号,标出具体观察模式
原标题:CommunityToolkit.MVVM send message identified by token to specific view model

我需要社区工具包的帮助。 MVVM利用信使进行不同观点的沟通。 我的问题是,与此同时,用户可以两次(因为台式照相机)对送信员的 t工作进行简单执行,我需要登记我的看法模式,而只是为了特定观点模式而倾听(我很难找到文件上的任何实例)。

Thank you for any help in advance.

I tried to simplify the code to include only needed parts of it.

用户ListView.xaml

<UserControl x:Class="ProjectIdeas.Core.Users.View.UsersListView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
             xmlns:viewmodel="clr-namespace:ProjectIdeas.Core.Users.ViewModel"
             xmlns:local="clr-namespace:ProjectIdeas.Core.Users.View"
             mc:Ignorable="d">

    <UserControl.DataContext>
        <viewmodel:UsersListViewModel />
    </UserControl.DataContext>

    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <syncfusion:SfDataGrid
            ItemsSource="{Binding Users}"
            SelectedItem="{Binding SelectedUser}" />

        <local:UserDetailView Grid.Column="1" />

    </Grid>

</UserControl>

用户ListViewModel.cs

public sealed partial class UsersListViewModel : ObservableObject
{

    [ObservableProperty]
    private UserModel? _selectedUser;

    partial void OnSelectedUserChanged(UserModel? value)
    {
        WeakReferenceMessenger.Default.Send(new UserDetailViewSelectedUserChangedMessage(value));
    }

}

用户

public sealed partial class UserDetailViewModel : ObservableObject, IRecipient<UserDetailViewSelectedUserChangedMessage>
{

    [ObservableProperty]
    private UserModel? _user;

    public UserDetailViewModel()
    {
        WeakReferenceMessenger.Default.Register<UserDetailViewSelectedUserChangedMessage>(this);
    }

    public void Receive(UserDetailViewSelectedUserChangedMessage message)
    {
        User = message.Value;
    }

}

用户详细意见

public class UserDetailViewSelectedUserChangedMessage : ValueChangedMessage<UserModel?>
{
    public UserDetailViewSelectedUserChangedMessage(UserModel? value) : base(value) {}
}
问题回答

IMHO, a UserControl 不应有“观点”,特别是直接如此,而“国际水道测量组织”应再次加以执行,而不见“观点”,而是在“表面”上这样做。

与此类似。

<>UsersListView.xaml.cs

public sealed partial class UsersListView: UserControl
{
    public static readonly DependencyProperty UsersProperty =
        DependencyProperty.Register(
            nameof(Users),
            typeof(object),
            typeof(UsersListView),
            new PropertyMetadata(default));

    public static readonly DependencyProperty SelectedUserProperty =
        DependencyProperty.Register(
            nameof(SelectedUser),
            typeof(UserModel),
            typeof(UsersListView),
            new PropertyMetadata(default));

    public UsersListView()
    {
        this.InitializeComponent();
    }


    public object Users
    {
        get => (object)GetValue(UsersProperty);
        set => SetValue(UsersProperty, value);
    }

    public UserModel SelectedUser
    {
        get => (UserModel)GetValue(SelectedUserProperty);
        set => SetValue(SelectedUserProperty, value);
    }
}

<>UsersListView.xaml

<syncfusion:SfDataGrid
    ItemsSource="{x:Bind Users, Mode=OneWay}"
    SelectedItem="{x:Bind SelectedUser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

<local:UserDetailView
    Grid.Column="1"
    User="{x:Bind SelectedUser, Mode=OneWay}" />




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签