将 mvvm-light CustomEvent 触发器与 ApplicationBarIconButton 结合使用
下面的按钮示例代码将触发第二页的打开。
<Button x:Name="btnSelect" Content="Select" HorizontalAlignment="Right" Margin="0,8,20,6"
Grid.Row="2" Width="200">
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand x:Name="btnSelectClicked"
Command="{Binding SelectEventPageCommand, Mode=OneWay}"/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</Button>
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void ContentGrid_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
Messenger.Default.Register<GoToPageMessage>(this, (action) => ReceiveMessage(action));
}
private object ReceiveMessage(GoToPageMessage action)
{
StringBuilder sb = new StringBuilder("/Views/");
sb.Append(action.PageName);
sb.Append(".xaml");
NavigationService.Navigate(
new System.Uri(sb.ToString(),
System.UriKind.Relative));
return null;
}
}
http://galasoft.ch/mvvm/getstarted/
任何人都可以建议我如何使用应用程序栏图标按钮?我收到错误属性“触发器”无法附加到 ApplicationBarIconButton 类型的元素。
或者我应该只使用CodeBehind?
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="False">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1">
</shell:ApplicationBarIconButton>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
The sample code below for a button will trigger opening of a second page.
<Button x:Name="btnSelect" Content="Select" HorizontalAlignment="Right" Margin="0,8,20,6"
Grid.Row="2" Width="200">
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand x:Name="btnSelectClicked"
Command="{Binding SelectEventPageCommand, Mode=OneWay}"/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</Button>
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void ContentGrid_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
Messenger.Default.Register<GoToPageMessage>(this, (action) => ReceiveMessage(action));
}
private object ReceiveMessage(GoToPageMessage action)
{
StringBuilder sb = new StringBuilder("/Views/");
sb.Append(action.PageName);
sb.Append(".xaml");
NavigationService.Navigate(
new System.Uri(sb.ToString(),
System.UriKind.Relative));
return null;
}
}
http://galasoft.ch/mvvm/getstarted/
Can anyone suggest how I can do the same using an ApplicationBarIconButton? I get an error Property 'Triggers' is not attachable to elements of type ApplicationBarIconButton.
Or should I just use CodeBehind?
<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="False">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1">
</shell:ApplicationBarIconButton>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Yeap ApplicationBar 与 Silverlight 中的其他控件略有不同。我能够通过使用以下命令来使用 ICommand:
http:// blog. humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx
或 Silverlight 工具包,它提供了一些扩展,如本答案所述:
如何添加wp7 中的应用程序栏到用户控件,但我从未使用过它。
Yeap ApplicationBar is a little bit different fom other controls in Silverlight. I was able to use an ICommand by using this one:
http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx
or the Silverlight toolkit which offers some extensions as stated in this answer:
How to add an application bar to a user control in wp7 but I never used it.
ApplicationBar
是由操作系统提供的服务,即不是框架的一部分,并且不支持触发器
,正如您已经发现的那样。如上所述。有许多解决方案可以解决此问题。或者,您可以使用 Silverlight Windows Phone 中的
ApplicationBarButtonCommand
和ApplicationBarButtonNavigation
行为工具包。如果您需要的话,创建ApplicationBarMenuCommand
是一项非常简单的任务。在您的情况下,要使用ApplicationBar
按钮导航到页面,则ApplicationBarButtonNavigation
行为即可解决问题。The
ApplicationBar
is a service that is provided by the operating system, i.e. not part of the Framework, and doesn't supportTriggers
as you have already discovered.As mentioned above there are a number of solutions that provide workarounds for this problem.Alternatively, you could use the
ApplicationBarButtonCommand
andApplicationBarButtonNavigation
behaviors from the Silverlight Windows Phone Toolkit. It's a simple enough task to create yourApplicationBarMenuCommand
if you need one. In your case, for using anApplicationBar
button to navigate to a page, then theApplicationBarButtonNavigation
behavior will do the trick.