首页 文章

Windows Phone自定义活动磁贴未按预期工作

提问于
浏览
2

我创建了一个Windows Phone应用程序,我需要创建自定义磁贴并从程序更新它

我创建了一个用户控件,

<TextBlock x:Name="Count" Foreground="White" FontFamily="Segoe WP Bold" FontSize="80" Text="{Binding Count}" HorizontalAlignment="Right" Margin="0,0,10,0"/>
 <TextBlock HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="0,0,0,0" Grid.Row="1" Text="Tile Demo" Foreground="White" FontFamily="Segoe WP Semibold" FontSize="35" />

从我的MainPage.xaml.cs文件我绑定计数然后我将用户控件转换为.jpg文件并存储在ISO存储中 . 如下

public void CreateOrUpdateTile(int count) 
 {
 CustomNotificationTile frontTile = new CustomNotificationTile();
 TileData tileData = new TileData() { Count = count };
 frontTile.DataContext = tileData;

 //frontTile.Count.Text = count.ToString();

 frontTile.Measure(new Size(173, 173));
 frontTile.Arrange(new Rect(0, 0, 173, 173));
 var bmp = new WriteableBitmap(173, 173);
 bmp.Render(frontTile, null);
 bmp.Invalidate();

 var isf = IsolatedStorageFile.GetUserStoreForApplication();
 var filename = "/Shared/ShellContent/Tile.jpg";

 if (!isf.DirectoryExists("/Shared/ShellContent"))
 {
        isf.CreateDirectory("/Shared/ShellContent");
 }

 using (var stream = isf.OpenFile(filename, System.IO.FileMode.OpenOrCreate))
 {
 bmp.SaveJpeg(stream, 173, 173, 0, 100);
 }


 ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("TileID=2"));

 //test if Tile was created
 if (TileToFind == null)
 {
       StandardTileData NewTileData = new StandardTileData
       {
             BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute),
             BackBackgroundImage = new Uri("Application_TileImage_173x173.png", UriKind.Relative)
       };

       ShellTile.Create(new Uri("/MainPage.xaml?TileID=2", UriKind.Relative), NewTileData);
       }

 #region Update the Tile Not Working as expected

        else
        {
            StandardTileData NewTileData = new StandardTileData
            {
                BackgroundImage = new Uri("isostore:" + filename, UriKind.Absolute)
            };
            TileToFind.Update(NewTileData);
        }

        #endregion
  }

Problem:

我的问题是我首先从构造函数调用创建具有一些虚拟数据的tile,它按预期工作

但是当我尝试从其他方法更新Tile时它不起作用,我从page_load事件调用的那些方法

这些方法将从WCF服务获取数据,我正在从WCF服务方法的已完成事件更新Tile,如下所示

Service.getProductsCountAsync();

Service.getProductsCountCompleted += (o,e) => { 
int count = e.Result;
Dispatcher.BeginInvoke(() =>
        {
            CreateOrUpdateTile(count);
        });
};

当控制器点击上面的代码时,我只看到黑色背景的数字,平铺计数正在更新,但背景颜色和标识正在发生变化,我不知道为什么会发生这种情况但需要尽早解决 .

I think the problem might be updating the UI from the background thread, but i don't know how to overcome that

以下是在方法调用之前和方法调用之后拍摄的图像

之前

Before Updating

之后

After service call

1 回答

  • 1

    它的工作

    我们必须绑定背景颜色,即使它们是静态的,它们也会动态地计数

    After that it is working as expected

相关问题