Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在应用程序运行之后运行命令_C#_Xaml_Windows Phone 8 - Fatal编程技术网

C# 在应用程序运行之后运行命令

C# 在应用程序运行之后运行命令,c#,xaml,windows-phone-8,C#,Xaml,Windows Phone 8,我注意到它不仅发生在一个项目中,而且也发生在多个项目中,所以我将提供一个简单的示例。我有这样的xaml: <Page x:Class="TestApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:TestApp" xmlns:d="htt

我注意到它不仅发生在一个项目中,而且也发生在多个项目中,所以我将提供一个简单的示例。我有这样的xaml:

<Page
x:Class="TestApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <Button Content="Button" Command="{Binding PressedButton}" HorizontalAlignment="Left" Margin="0,-10,0,-9" VerticalAlignment="Top" Height="659" Width="400"/>
</Grid>
</Page>

这很奇怪,但PressedButton只在应用程序启动时运行(在启动时运行不是很奇怪吗?)。之后,即使点击按钮,也不会触发任何内容。我想你可能是因为每次调用“getter”时都返回一个新命令而导致绑定问题。尝试在构造函数中设置一次命令(例如)

SaveNote()
方法中,可以测试值并保存(或不保存):

public abstract class ObservableObject : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            this.PropertyChanged(this, e);
        }
    }
}

public class Command : ICommand
{
    private Action<object> action;

    public Command(Action<object> action)
    {
        this.action = action;
    }

    public bool CanExecute(object parameter)
    {
        if (action != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        if (action != null)
        {
            action((string)parameter);
        }
    }
}

public class TestViewModel : ObservableObject
{
    public ICommand PressedButton
    {
        get
        {
            return new Command((param) => { });
        }
    }
}
    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
        DataContext = new TestViewModel();
    }
public MainPage()
{
    PressedAdd = new Command(param => SaveNote());
}

public ICommand PressedAdd { get; private set; }
private void SaveNote()
{
    if (NoteTitle == null || NoteContent == null)
        return;

    // Do something with NoteTitle and NoteContent
}