亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 C# 中的 ViewModelHelper 關閉窗口(XAML)?

如何從 C# 中的 ViewModelHelper 關閉窗口(XAML)?

C#
吃雞游戲 2023-12-17 10:34:27
我想從onTimeOutCommand()方法中執行onCloseCommand(object sender)方法,但我不知道如何傳遞從該方法傳遞的所需參數?請參考以下代碼片段。XAML 代碼:x:name = "Recorder" // window name define in the begining//below command is used for closing this window when user clicks on close buttonCommand = "{Binding CloseCommand}" CommandParameter="{Binding ElementName=Recorder}"視圖模型代碼:CloseCommand = new DelegateCommand<object>(helper.onCloseCommand);ViewModelHelper代碼:Note: onCloseCommand() methodis working as per expectationonCloseCommand(object sender) // This method is used for closing the window on clicking on close button of this window{    if(sender != null && send is window)    {         (sender as window).close();    }}onTimeOutCommand() // this method is used for closing the window (the window which is passed in onCloseCommand() method) automaticlly after time out of the recording{      how to perform onCloseCommand() from this method?}
查看完整描述

2 回答

?
米脂

TA貢獻1836條經驗 獲得超3個贊

您應該使用AttachProperty關閉窗口。


public static class Attach

{

    #region CloseProperty

    public static DependencyProperty WindowCloseProperty = DependencyProperty.RegisterAttached("WindowClose",

        typeof(bool), typeof(Attach),

        new UIPropertyMetadata(false, WindowClosePropertyChangedCallback));


    private static void WindowClosePropertyChangedCallback(DependencyObject dependencyObject,

        DependencyPropertyChangedEventArgs eventArgs)

    {

        var window = (Window)dependencyObject;

        if (window != null && (bool)eventArgs.NewValue)

            window.Close();

    }


    public static bool GetWindowClose(DependencyObject obj)

        => (bool)obj.GetValue(WindowCloseProperty);


    public static void SetWindowClose(DependencyObject obj, bool value)

    {

        obj.SetValue(WindowCloseProperty, value);

    }


    #endregion

}

并在 XAML 中


<Window x:Class="MyProject.MyWindow"

xmlns:helper="clr-namespace:MyProject.Helper;assembly=MyProject"

    helper:Attach.WindowClose="{Binding IsWindowClose}">

并且在 ViewModel 當您將 IsWindowClose 設置為 true 時關閉窗口


  public bool IsWindowClose

  {

      get => _isWindowClose;

      set => SetProperty(ref _isWindowClose, value);

  }


查看完整回答
反對 回復 2023-12-17
?
catspeake

TA貢獻1111條經驗 獲得超0個贊

如何從此方法執行 onCloseCommand() ?


您有多種選擇,例如創建一個附加行為,在一定的超時后關閉窗口,并使用 xaml 中的行為,從而有效地將視圖模型排除在外。


我建議您在視圖模型中對窗口的 Loaded 事件做出反應并存儲窗口并在以后想要關閉它時使用它。


<Window x:Class="MyWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

        mc:Ignorable="d">

     <i:Interaction.Triggers>

         <i:EventTrigger EventName="Loaded">

             <i:InvokeCommandAction Command="{Binding WindowLoadedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>

         </i:EventTrigger>

     </i:Interaction.Triggers>


     <!--- the rest of the window here --->

</Window>

但是讓我向您推薦這個答案,以獲取一種對 mvvm 更友好且更重要的是可測試的方法來從查看模型:


使窗口實現一個接口并存儲和使用它(不是完整的Window)!


因此 WindowLoadedCommand 的類型為 DelegateCommand<IClosable> 并將 IClosable 存儲在字段中。發生超時時,從字段中獲取 IClosable 并調用 Close。


查看完整回答
反對 回復 2023-12-17
  • 2 回答
  • 0 關注
  • 234 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號