【问题标题】:How to correctly pin a HubTile to the start screen如何正确地将 HubTile 固定到开始屏幕
【发布时间】:2012-08-15 13:57:04
【问题描述】:

我希望能够将应用程序中的 hubtile 固定到开始屏幕,以便开始屏幕 tile 具有与我的 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 数据创建辅助磁贴?

【问题讨论】:

  • 到底是什么失败了?你的代码看起来不错
  • CreateLiveTile() 方法中创建LiveTile 时出现NullReferenceException。至于您在下面的回答,我假设您的意思是目前不可能将 HubTile 固定到开始屏幕并具有相同的效果?如果是这样,除了正常的前后翻转动画之外,您是否知道任何方式来为开始屏幕磁贴设置动画?
  • 抱歉,Windows Phone 7.5 不支持更多动画。异常发生在哪一行?
  • StandardTileData LiveTile = new StandardTileData{ .. } 上,当我尝试获取标题和消息等 hubtile 的属性时,它们为空。据我所知,这是因为我试图在MenuItem_Tap 事件处理程序中获取这些属性并将其传递给不起作用的CreateLiveTile(HubTile hubtile) 参数。我仍然想创建普通的辅助图块,并相应地更改了我的实现,但我不确定如何仍然获得 hubtile 属性?这可以在MenuItem_Tap 中完成吗?如果没有,您有什么建议吗?
  • 我研究了以某种方式获取对发生上下文菜单单击的特定 hubtile 的引用,并以这种方式获取 hubtile 属性,但我找不到任何资源或弄清楚如何做到这一点?

标签: c# windows-phone-7 xaml


【解决方案1】:

我希望能够将应用程序中的 hubtile 固定到开始屏幕,以便开始屏幕 tile 具有与我的 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.
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-26
    • 2013-01-29
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多