MVVM Windows 8 appBar событие mvvm

Моя проблема связана с событием. В AppBar все события, которые я придумывал, не работали. (MessageDialog или другое событие), когда AppBar отображается, я не могу скрыть, а в AppBar не работает нажатие кнопки.

<Page.BottomAppBar>
<AppBar x:Name="AppBar" Background="#FF1DB05F">         
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">                  
        <Button x:Name="SaveButton" Style="{StaticResource AppBarButtonStyle}"
         Content="&#xE105;"

         AutomationProperties.Name="Save" >                       
           <WinRtBehaviors:Interaction.Behaviors>        
                <Win8nl_Behavior:EventToCommandBehavior Event="Tapped"

                                          Command="NewFileXml"

                                          />     
            </WinRtBehaviors:Interaction.Behaviors>

        </Button>

В MainViewModel.cs

public async void NewFileXml()
        {

            XmlDocument dom = new XmlDocument();
            XmlComment comment = dom.CreateComment("This is Goal a Year");
            XmlElement x;
            dom.AppendChild(comment);
            x = dom.CreateElement("Goal of a Year");
            dom.AppendChild(x);

            XmlElement stepXml = dom.CreateElement("Goalyear");
            XmlElement goalYearXml = dom.CreateElement("GoalStep");
            stepXml.InnerText = GoalYear;
            goalYearXml.AppendChild(stepXml);

            Windows.Storage.StorageFolder sf = await Windows.ApplicationModel.Package.Current.InstalledLocation.CreateFolderAsync("GoalPlan");
            StorageFile st = await sf.CreateFileAsync("GoalYear.xml");
            await dom.SaveToFileAsync(st);
        }
        public  ICommand NewFile
        {
            get
            {
                return new RelayCommand(() =>
                    {

                        NewFileXml();

                    });
            }
        }

Я сделал с помощью польского Microsoft. Может кто что добавит.

 private RelayCommand exampleContent;
            public RelayCommand ItIsBind
            {
                get
                {
                    return exampleContent ?? (exampleContent = new RelayCommand(ContentLoad));
                }
            }
**Method example**
public void ContentLoad()
{

}

person MichaelS    schedule 14.01.2013    source источник


Ответы (2)


Здесь есть несколько возможных проблем...

1) Ваша команда называется не "NewFileXml", а "NewFile"
2) Похоже, вы не устанавливаете свой DataContext - вы делаете это где-то еще (если да, то вы не показали это)

person ZombieSheep    schedule 14.01.2013
comment
Спасибо за этот ответ. Я меняю имя в MainPage.xaml. Однако это не помогло. Мой DataContex: DataContext={Binding Source={StaticResource Locator}} › - person MichaelS; 14.01.2013

Я не уверен, что это сработает для вас, но в моем случае мне пришлось разместить панель приложений внутри основной сетки. Итак, вы копируете это

<AppBar x:Name="AppBar" Background="#FF1DB05F">         
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">                  
    <Button x:Name="SaveButton" Style="{StaticResource AppBarButtonStyle}"
     Content="&#xE105;"

     AutomationProperties.Name="Save" >                       
       <WinRtBehaviors:Interaction.Behaviors>        
            <Win8nl_Behavior:EventToCommand etc...

без

<Page.BottomAppBar> 

теги и вставьте его в теги основной сетки.

person Landvis    schedule 20.02.2013
comment
Почему? Второй способ лучше, т.к. все управление работает и Win8nl мне не понадобился, нужен только RelayCommand. - person MichaelS; 20.02.2013