我有以下问题:
在应用程序中,当用户返回计算机时,我必须处理该事件并向他显示一个模态窗口,在其中提供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}"
ViewModel:
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();
});
}
事件已触发,显示了页面,但视图模型未附加到页面,并且不响应按钮的按下。但是,关闭窗口的OnAddButtonClick等服务命令会对按下按钮做出反应。 如何绑定视图和视图模型,以便在视图模型中按下按钮可以正常工作? 非常感谢那些回答的人
答案 0 :(得分:0)
问题是我尝试手动创建新的窗口/页面。对于绑定虚拟机和视图,应使用ViewDispatcher:
var ViewDispatcher = Mvx.Resolve<IMvxViewDispatcher>();
ViewDispatcher.ShowViewModel(newMvxViewModelRequest(typeof(UserReturnDialogViewModel)));