简体   繁体   English

在Windows Phone上使用HubTiles,PRISM和MVVM进行页面导航

[英]Page navigation using HubTiles, PRISM, and MVVM on Windows Phone

I'm trying to use the HubTile control from the WPToolkit, PRISM, and MVVM design. 我正在尝试使用WPToolkit,PRISM和MVVM设计中的HubTile控件。 My problem is navigating pages. 我的问题是浏览页面。

MainPageModel MainPageModel

private string _navigationUri;
        public string NavigationUri
        {
            get { return _navigationUri; }
            set
            {
                if (_navigationUri != value)
                {
                    NotifyPropertyChanging();
                    _navigationUri = value;
                    NotifyPropertyChanged();
                }
            }
        }

MainPageViewModel MainPageViewModel

        private void Navigate(string s)
    {
        (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri(s, UriKind.RelativeOrAbsolute));
    }

        private DelegateCommand<string> _hubTileTap;
    public DelegateCommand<string> HubTileTap
    {
        get { return _hubTileTap; }
    }

_hubTileTap = new DelegateCommand<string>(Navigate);

XAML XAML

<DataTemplate>
                        <toolkit:HubTile 
                            Margin="10"
                            Size="Default"
                            Title="{Binding Title}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Tap">
                                    <i:InvokeCommandAction 
                                        Command="{Binding DataContext.HubTileTap}"
                                        CommandParameter="{Binding NavigationUri}"></i:InvokeCommandAction>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </toolkit:HubTile>
                    </DataTemplate>

Putting in a breakpoint, I can see that my method never actually fires. 放置一个断点,我可以看到我的方法从未真正触发过。 Also, honestly, I'm not 100% certain that I set things up the right way to pass the string from the HubTile object to the DelegateCommand and then to the Method. 另外,说实话,我不确定100%是否正确设置了将字符串从HubTile对象传递给DelegateCommand然后传递给Method的正确方法。

I'm very new to MVVM, PRISM, and still a noob at programming. 我是MVVM,PRISM的新手,并且对编程还是不熟悉的。 Thanks for any help! 谢谢你的帮助!

What does data template live in? 数据模板包含在什么内容中? Do you have a collection of tiles? 你有瓷砖的收藏吗? The context of the template is the item usually. 模板的上下文通常是项目。 So, where Title lives is where the command will fire. 因此,Title所在的位置是命令将触发的位置。

edit: what you added, DataContext.HubTileTap won't work, inside the template, DataContext is the item. 编辑:您添加的内容,DataContext.HubTileTap将不起作用,在模板内部,DataContext是项。

One possible way to do this in with prism is to put an action on the item class. 使用Prism进行此操作的一种可能方法是对项目类进行操作。 Then when you create the items, set the action of each item to your tap method. 然后,在创建项目时,将每个项目的操作设置为点击方法。

public class EmptyClass
    {
        public ObservableCollection<HubTileItem> HubTiles { get; set; }

        public EmptyClass()
        {
            HubTiles = new ObservableCollection<HubTileItem>();
            HubTiles.Add(new HubTileItem { Title = "Tile1", HubTileTap = () => HubTileTap() };);
        }

        public void HubTileTap()
        {
        }
    }

    public class HubTileItem
    {
        public string Title { get; set; }

        public Action HubTileTap { get; set; }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM