UWP x:Bind 命令
UWP x:Bind command
是否可以遵守命令。
上下文
<Page.DataContext>
<local: MainPage />
</Page.DataContext>
命令
static public Command Pause (MediaElement element)
{
element.Pause ();
return new Command ();
}
绑定本身
<Button VerticalAlignment = "Bottom"
Margin = "10,0,0,10" Command = "{x: Bind local: MainPage.Pause (mp)}"
HorizontalAlignment = "Left" Height = "30" Width = "100"> Pause </ Button>
开始但几秒钟后出错
System.WhosebugException
为什么会出现溢出错误以及如何克服
当您使用{x:bind} to bind the ButtonBase.Command 属性时,您应该绑定一个命令,该命令将在按下此按钮时调用。在你的代码中,你绑定了一个静态方法,其中 return 一个命令,但是这个静态方法属于类型本身而不是属于特定的对象。
要解决这个错误,您应该删除xaml中设置页面数据上下文对象实例的代码。也就是删除下面的代码,
<Page.DataContext>
<local:MainPage />
</Page.DataContext>
如果你想绑定一个命令来操作MediaElement
,你应该把操作代码放在命令中,这里是一个例子,
MainPage.xaml,
<StackPanel>
<MediaElement Name="mp" Width="800" Height="400" Source="ms-appx:///Assets/video.mp4"/>
<Button VerticalAlignment = "Bottom" Margin = "10,0,0,10" Command = "{x:Bind local:MainPage.Pause(mp)}"
HorizontalAlignment = "Left" Height = "30" Width = "100">Pause</Button>
</StackPanel>
MainPage.xaml.cs 和命令,
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
public static Command Pause(MediaElement element)
{
//element.Pause();
return new Command(s => { element.Pause(); },true);
}
}
public class Command : ICommand
{
private Action<object> action;
private bool canExecute;
public event EventHandler CanExecuteChanged;
public Command(Action<object> action,bool canExecute)
{
this.action = action;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute;
}
public void Execute(object parameter)
{
action(parameter);
}
}
是否可以遵守命令。
上下文
<Page.DataContext>
<local: MainPage />
</Page.DataContext>
命令
static public Command Pause (MediaElement element)
{
element.Pause ();
return new Command ();
}
绑定本身
<Button VerticalAlignment = "Bottom"
Margin = "10,0,0,10" Command = "{x: Bind local: MainPage.Pause (mp)}"
HorizontalAlignment = "Left" Height = "30" Width = "100"> Pause </ Button>
开始但几秒钟后出错 System.WhosebugException 为什么会出现溢出错误以及如何克服
当您使用{x:bind} to bind the ButtonBase.Command 属性时,您应该绑定一个命令,该命令将在按下此按钮时调用。在你的代码中,你绑定了一个静态方法,其中 return 一个命令,但是这个静态方法属于类型本身而不是属于特定的对象。
要解决这个错误,您应该删除xaml中设置页面数据上下文对象实例的代码。也就是删除下面的代码,
<Page.DataContext>
<local:MainPage />
</Page.DataContext>
如果你想绑定一个命令来操作MediaElement
,你应该把操作代码放在命令中,这里是一个例子,
MainPage.xaml,
<StackPanel>
<MediaElement Name="mp" Width="800" Height="400" Source="ms-appx:///Assets/video.mp4"/>
<Button VerticalAlignment = "Bottom" Margin = "10,0,0,10" Command = "{x:Bind local:MainPage.Pause(mp)}"
HorizontalAlignment = "Left" Height = "30" Width = "100">Pause</Button>
</StackPanel>
MainPage.xaml.cs 和命令,
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
public static Command Pause(MediaElement element)
{
//element.Pause();
return new Command(s => { element.Pause(); },true);
}
}
public class Command : ICommand
{
private Action<object> action;
private bool canExecute;
public event EventHandler CanExecuteChanged;
public Command(Action<object> action,bool canExecute)
{
this.action = action;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute;
}
public void Execute(object parameter)
{
action(parameter);
}
}