如何正确地将HubTile固定到开始屏幕

时间:2012-08-15 01:52:22

标签: c# windows-phone-7 xaml

我希望能够将我的应用程序中的hubtile固定到开始屏幕,以便开始屏幕图块具有与我的hubtile相同的动画效果。当用户点击固定的hubtile时,他或她将被导航到正确的页面。我已按照此示例http://igrali.com/2011/09/27/how-to-pin-a-hubtile-to-start-screen/,但它对我不起作用?

MainPage.xaml中

<ListBox Grid.Row="0" x:Name="tileList" toolkit:TiltEffect.IsTiltEnabled="True">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <toolkit:WrapPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <toolkit:HubTile Title="{Binding Title}" Margin="3"
                                         Notification="{Binding Notification}"
                                         DisplayNotification="{Binding DisplayNotification}"
                                         Message="{Binding Message}"
                                         GroupTag="{Binding GroupTag}"
                                         Source="{Binding ImageUri}"
                                         Tap="hubTile_Tap">
                            <toolkit:ContextMenuService.ContextMenu>
                                <toolkit:ContextMenu x:Name="menu">
                                    <toolkit:MenuItem Header="pin to start" Tap="MenuItem_Tap"/>
                                </toolkit:ContextMenu>
                            </toolkit:ContextMenuService.ContextMenu>
                        </toolkit:HubTile>

                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

MainPage.xaml.cs中

public MainPage()
    {
        InitializeComponent();

        CreateHubTiles();
    }

private void CreateHubTiles()
    {
        List<TileItem> tileItems = new List<TileItem>() 
        {
            new TileItem() { ImageUri = "/Images/shareStatusImage.jpg", Title = "status", /*Notification = "last shared link uri",*/ Message = Settings.statusMessage.Value, GroupTag = "TileGroup" },
            new TileItem() { ImageUri = "/Images/shareLinkImage.jpg", Title = "link", /*Notification = "last shared status message",*/ Message = "last shared link uri", GroupTag = "TileGroup" }, 
        };

        this.tileList.ItemsSource = tileItems;
    }

private void MenuItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        HubTile tap = sender as HubTile;

        CreateLiveTile(tap);
    }

    private void CreateLiveTile(HubTile hubtile)
    {
        //First create a new StandardTileData object from the hubtile to be pinned

        //NullReferenceException occurs here!!
        StandardTileData LiveTile = new StandardTileData
        {
            BackBackgroundImage = ((System.Windows.Media.Imaging.BitmapImage)hubtile.Source).UriSource,
            Title = hubtile.Title,
            BackTitle = hubtile.Title,
            BackContent = hubtile.Message
        };    

        //Check to see if the tile already exists
        //If not, create it
        //If yes, display messagebox
        ShellTile Tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=" + LiveTile.Title));

        if (Tile == null)
        {
            try
            {
                ShellTile.Create(new Uri("/MainPage.xaml?DefaultTitle=" + LiveTile.Title, UriKind.Relative), LiveTile);
            }
            catch (Exception)
            {
                MessageBox.Show("This tile could not be pinned", "Warning", MessageBoxButton.OK);
            }
        }
        else
        {
            MessageBox.Show("This tile has already been pinned", "Notice", MessageBoxButton.OK);

        }
    }

我被困在这个以及如何改变它以便它可以工作!?此外,这实际上是在开始屏幕上放置一个hubtile,还是只使用hubtile数据创建一个辅助磁贴?

1 个答案:

答案 0 :(得分:2)

  

我希望能够将我的应用程序中的hubtile固定到开始屏幕,以便开始屏幕图块具有与我的hubtile相同的动画效果

“普通”应用(不是第一方应用)的动态磁贴在他们可以做的事情上受到限制。您只能提供front/back image, front/back text a back content, and a count。这就是我们可以访问的全部内容。

更新根据收到的错误添加更多信息 您可以通过获取TileItem

来实现此目的
void MenuItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    var menuItem = (MenuItem)sender;
    var tileItem = menuItem.DataContext as TileItem;
    CreateLiveTile(tileIte);
}
enter code here
private void CreateLiveTile(TileItem item)
{
    // use the TileItem, not HubTile.
}