gpt4 book ai didi

c# - ViewModel 没有绑定(bind)到 View MvvmCross

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:47 25 4
gpt4 key购买 nike

我有以下问题:

在应用程序中,当用户返回计算机时,我必须处理该事件并向他显示一个模态窗口,在该窗口中提供 3 个可能的答案。

我使用该服务执行此操作:

public WinUserReturnDialogServiceImpl(ISettingsManager 
settingsManager) : base(settingsManager)
{
_dialogPage = new UserReturnDialogPage();
_dialogPage.AddButton.Click += OnAddButtonClick;
_dialogPage.DivideButton.Click += OnDivideButtonClick;
_dialogPage.CancelButton.Click += OnCancelButtonClick;

_dialogWindow = new DialogWindow()
{
Content = _dialogPage
};
}

protected override void ShowCustomDialog(UserReturnDialogData dialogData)
{

_pauseDuration = Math.Floor(dialogData._userAfkMinuites);
Application.Current.Dispatcher.Invoke(() =>
{
_dialogPage.AFKMessage.Text = string.Format("Вы отсутствовали {0} мин", _pauseDuration);
_dialogWindow.Show();
});
return;
}

private void OnAddButtonClick(object sender, RoutedEventArgs e)
{
CloseCustomDialog();
}

后面的页面代码:

//[MvxViewFor(typeof(UserReturnDialogViewModel))]
//[WinContentPresentation(IsSheet = true, TransitionForwardType = TransitionType.ToRight, TransitionReturnType = TransitionType.FromRigth, WindowIdentifier = nameof(DialogWindow))]
[MvxContentPresentation(WindowIdentifier = nameof(DialogWindow), StackNavigation = false)]
public partial class UserReturnDialogPage : MvxWpfView<UserReturnDialogViewModel>
{

public UserReturnDialogPage()
{
InitializeComponent();

}

和页面 xaml:

<views:MvxWpfView
x:Class="SmlHours.Win.Presentation.Views.Pages.Dialogs.UserReturnDialogPage"
x:TypeArguments="vm:UserReturnDialogViewModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
xmlns:vm="clr-namespace:SmlHours.Core.Presentation.ViewModels.Dialogs;assembly=SmlHours.Core"
...
<Button
x:Name="AddButton"
HorizontalAlignment="Left"
Visibility="Visible"
Width="85"
Height="32"
Template="{DynamicResource BaseNavigationButtonTemplate}"
Command="{Binding AddTimeAfterUserReturn}"

View 模型:

public class UserReturnDialogViewModel : BaseViewModel
{

private IMonitoringInteractor _monitoringInteractor;

public IMvxCommand AddTimeAfterUserReturn { get; private set; }

public UserReturnDialogViewModel(IMonitoringInteractor monitoringInteractor)
{
_monitoringInteractor = monitoringInteractor;
AddTimeAfterUserReturn = CreateAsyncCommand(AddTimeAfterReturnTask);
}

//I need to fire this command after button pressed!!!
private Task AddTimeAfterReturnTask() => Task.Run(async () =>
{
var model = _monitoringInteractor.FixationTimeAfterUserReturn();
});
}

事件被触发,页面被显示,但是 View 模型没有附加到页面并且不响应按钮按下。但是,关闭窗口的 OnAddButtonClick 等服务命令会对按下按钮使用react。如何绑定(bind) View 和 View 模型,以便按下按钮在 View 模型中起作用?非常感谢回复的人

最佳答案

问题是我尝试手动创建新的窗口/页面。要绑定(bind) vm 和 View ,应使用 ViewDispatcher:

 var ViewDispatcher = Mvx.Resolve<IMvxViewDispatcher>();
ViewDispatcher.ShowViewModel(newMvxViewModelRequest(typeof(UserReturnDialogViewModel)));

关于c# - ViewModel 没有绑定(bind)到 View MvvmCross,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54985476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com