将 mvvm-light CustomEvent 触发器与 ApplicationBarIconButton 结合使用

发布于 2024-10-14 19:59:21 字数 1938 浏览 3 评论 0原文

下面的按钮示例代码将触发第二页的打开。

            <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2024-10-21 19:59:21

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.

白首有我共你 2024-10-21 19:59:21

ApplicationBar 是由操作系统提供的服务,即不是框架的一部分,并且不支持触发器,正如您已经发现的那样。如上所述。有许多解决方案可以解决此问题。

或者,您可以使用 Silverlight Windows Phone 中的 ApplicationBarButtonCommandApplicationBarButtonNavigation 行为工具包。如果您需要的话,创建 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 support Triggers 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 and ApplicationBarButtonNavigation behaviors from the Silverlight Windows Phone Toolkit. It's a simple enough task to create your ApplicationBarMenuCommand if you need one. In your case, for using an ApplicationBar button to navigate to a page, then the ApplicationBarButtonNavigation behavior will do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文