簡體   English   中英

WinRT 8.1:如何在viewModel上調用方法后隱藏Flyout

[英]WinRT 8.1: How to hide Flyout after calling a method on the viewModel

我的Win 8.1應用程序中有一個彈出窗口,正在使用約定在viewModel上調用方法。 很好 調用方法后,如何隱藏/關閉“彈出”? 使用后台代碼,它將是f.hide()但我無法通過Caliburn.Micro弄清楚。

這是我的XAML和ViewModel方法。

<Button Content="Add Income / Expense"  Margin="35,0,0,0">
                <Button.Flyout>
                    <Flyout>
                        <StackPanel>
                            <TextBox Name="LabelToAdd" Header="Enter Label name of income / expense:"/>
                            <StackPanel Orientation="Horizontal">
                                <Button Name="btn_Add" Content="Add"></Button>
                                <Button Name="btn_Cancel" Content="Cancel"></Button>
                            </StackPanel>
                        </StackPanel>
                    </Flyout>
                </Button.Flyout>
            </Button>

方法:

    public void btn_Add()
    {

        _income.Add(new Transaction (_labelToAdd,  10.00M, DateTime.Now));
        _labelToAdd = string.Empty;
        NotifyOfPropertyChange(() => LabelToAdd);
    // Hiding the Flyout here?
    }

非常感謝馬丁的任何投入。

我所做的並且似乎正在運行的操作是在彈出視圖模型上創建的type action屬性

   public Action CloseFlyout { get; set; }

在我的調用視圖模型中,將其設置為hide方法

  var regionflyout = new RegionFlyout();
  var vm = regionflyout.DataContext as RegionViewModel;
  vm.CloseFlyout = ()=> regionflyout.Hide();

  regionflyout.ShowIndependent();

然后最終在我的Flyout視圖模型中調用CloseFlyout操作。

  this.CloseFlyout.Invoke();

不確定這種方法有多大,但似乎可行。

您可以將彈出的輝石綁定到屬性。 然后在方法中將其設置為false:

private bool showFlyout;

public bool ShowFlyout
{
    get
    { 
        return showFlyout;
    }
    set
    {
        showFlyout = value; 
        PropertyChanged("ShowFlyout");
    }
}

public void btn_Add()
{
    _income.Add(new Transaction (_labelToAdd,  10.00M, DateTime.Now));
    _labelToAdd = string.Empty;
    NotifyOfPropertyChange(() => LabelToAdd);
    ShowFlyout = false;
}

假定您的類實現了INotifyPropertyChanged接口。 同樣,對於C#中的方法,btn_Add()也不是很好的標識符。 最好使用PascalCase。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM