Windows 8 Live Hub Tile

本文关键字:Tile Hub Live Windows | 更新日期: 2023-09-27 18:31:31

我正在使用 Callisto Toolkit 在 Windows 8 应用程序中创建动态中心磁贴,但我无法为我的动态磁贴执行数据绑定。我使用的代码如下:

<callisto:LiveTile x:Name="liveTile2" 
            Background="#3B5998"
            ItemsSource="{Binding Tile}"
            Grid.Column="5"
            Grid.Row="1"
            Height="250"
            VerticalAlignment="Top"
            BorderBrush="White" BorderThickness="1"
            Margin="5" 
            Direction="Up">
            <callisto:LiveTile.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="5">
                        <Grid.DataContext>
                            <ViewModel:Tile/>
                        </Grid.DataContext>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Image x:Name="tileImage" Source="{Binding Image}"/>

                        <TextBlock Foreground="White"
                            TextWrapping="Wrap"
                            TextTrimming="WordEllipsis" 
                            FontSize="14"
                            Grid.Row="1" Margin="0,71,0,0" Text="{Binding Msg}" />

                    </Grid>
                </DataTemplate>
            </callisto:LiveTile.ItemTemplate>
            <callisto:LiveTile.DataContext>
                <ViewModel:LiveTileData/>
            </callisto:LiveTile.DataContext>
        </callisto:LiveTile>

这是磁贴的类:

namespace Test.ViewModel
{
    public class Tile
    {
        public string Msg { get; set; }
        public Uri Image { get; set; }
    }
    public class LiveTileData
    {
        private static ObservableCollection<Tile> tileData = new ObservableCollection<Tile>();
        public static ObservableCollection<Tile> TileData
        {
            get
            {
                return tileData;
            }
        }
    }
}

Windows 8 Live Hub Tile

绑定不正确。您应该将LiveTileItemsSource绑定为ObservableCollection<Tile> TileData。此外,DataContext传递给LiveTile本身,而不是传递给其模板。查看下面的代码。

XAML

<callisto:LiveTile x:Name="liveTile2" 
    Background="#3B5998"
    ItemsSource="{Binding TileData}"
    Grid.Column="5"
    Grid.Row="1"
    Height="250"
    VerticalAlignment="Top"
    BorderBrush="White" BorderThickness="1"
    Margin="5" 
    Direction="Up">
    <callisto:LiveTile.ItemTemplate>
        <DataTemplate>
            <Grid Margin="5">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Image x:Name="tileImage" Source="{Binding Image}" Stretch="None"/>
                <TextBlock Foreground="White"
                    TextWrapping="Wrap"
                    TextTrimming="WordEllipsis" 
                    FontSize="14"
                    Grid.Row="1" Margin="0,71,0,0" Text="{Binding Msg}" />
            </Grid>
        </DataTemplate>
    </callisto:LiveTile.ItemTemplate>
    <callisto:LiveTile.DataContext>
        <ViewModel:LiveTileData />
    </callisto:LiveTile.DataContext>
</callisto:LiveTile>

C#

public class Tile
{
    public string Msg { get; set; }
    public Uri Image { get; set; }
}
public class LiveTileData
{
    public LiveTileData()
    {
        tileData.Add(new Tile { Msg = "Stack Overflow", Image = new Uri("http://jenswinter.com/image.axd?picture=stackoverflow-logo-250.png") });
        tileData.Add(new Tile { Msg = "Super User", Image = new Uri("http://superuser.com/content/superuser/img/logo.png") });
    }
    private static ObservableCollection<Tile> tileData = new ObservableCollection<Tile>();
    public static ObservableCollection<Tile> TileData
    {
        get
        {
            return tileData;
        }
    }
}