0

我有一个应用程序,我想在用户离开应用程序时更新动态磁贴。第一个问题是这是否是一个坏主意?如果用户每天启动应用程序 20 次,这会是一个坏主意还是会以任何方式影响后台服务?

第二个问题是这可能需要多长时间或资源密集型是否有限制。我想我会把这段代码放在 OnNavigatedFrom 中,如果动态磁贴的更新时间过长,操作系统会杀死应用程序吗?我需要创建一个图像,将其保存到独立存储,读取图像,然后更新磁贴。

真的很期待你对此的一些想法。

编辑1:我问的原因是,如果我像上面那样做它就可以了。但是,如果我在应用程序启动时立即退出应用程序,我只会得到一个黑色图块,而不是带有背景图像的图块。所以我觉得代码没有完成。我怎样才能避免这种情况?

编辑 2:由于我正在动态创建动态磁贴,我认为问题在于背景图像的加载。我加载背景图像,然后向其中添加文本。当磁贴变黑时,我仍然可以看到文本,因此加载的背景图像一定有一些东西用作磁贴的背景。

编辑 3:这是创建用作背景图像的图像的完整代码。我试图简化一点以减少代码。

Grid grid = new Grid();

StackPanel sp = new StackPanel();
sp.Height = 173;
sp.Width = 173;

sp.Background = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
strBackBackground = "";

StreamResourceInfo info;

sp.Background = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"];
strBackBackground = "";
info = Application.GetResourceStream(new Uri("/MyApp;component/images/Icons/livetile/metro-" + strSymbol + ".png", UriKind.Relative));

// create source bitmap for Image control (image is assumed to be alread 173x173)
WriteableBitmap wbmp3 = new WriteableBitmap(1, 1);
try
{
    wbmp3.SetSource(info.Stream);
}
catch
{
}

Image img3 = new Image();
img3.Source = wbmp3;
// add Image to Grid
img3.Width = 173;
img3.Height = 173;
img3.Margin = new Thickness { Left = 0, Bottom = 0, Right = 0, Top = 0 };

TextBlock txtTemperature = new TextBlock();
TextBlock txtTemperatureRing = new TextBlock();

txtTemperature.Foreground = new SolidColorBrush(Colors.White);
txtTemperature.Text = strTemp;
txtTemperature.TextAlignment = TextAlignment.Right;
txtTemperatureRing.Style = (Style)Application.Current.Resources["PhoneTextTitle3Style"];
txtTemperatureRing.FontFamily = new FontFamily("Segoe WP Light");
txtTemperatureRing.FontSize = 40;
txtTemperatureRing.Foreground = new SolidColorBrush(Colors.White);
txtTemperatureRing.Text = "°";
txtTemperatureRing.TextAlignment = TextAlignment.Right;

txtTemperature.FontFamily = new FontFamily("Segoe WP");
txtTemperature.FontSize = 40;
txtTemperature.Margin = new Thickness { Left = 0, Bottom = 0, Right = 0, Top = -55 };
txtTemperature.Height = 80;
txtTemperature.Width = 135;
txtTemperatureRing.Margin = new Thickness { Left = 130, Bottom = 0, Right = 0, Top = -112 };
txtTemperatureRing.Height = 50;
txtTemperatureRing.Width = 29;

sp.Children.Add(img3);
sp.Children.Add(txtTemperature);
sp.Children.Add(txtTemperatureRing);

//call measure, arrange and updatelayout to prepare for rendering
sp.Measure(new Size(173, 173));
sp.Arrange(new Rect(0, 0, 173, 173));
sp.UpdateLayout();
grid.Children.Add(sp);

WriteableBitmap wbmp = new WriteableBitmap(173, 173);
wbmp.Render(grid, null);
wbmp.Invalidate();

//write image to isolated storage
string sIsoStorePath = @"\Shared\ShellContent\tile.png";
using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
    //ensure directory exists
    String sDirectory = System.IO.Path.GetDirectoryName(sIsoStorePath);
    if (!appStorage.DirectoryExists(sDirectory))
    {
        appStorage.CreateDirectory(sDirectory);
    }

    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(sIsoStorePath, System.IO.FileMode.Create, appStorage))
    {
        wbmp.SaveJpeg(stream, 173, 173, 0, 100);
    }
}


/// If application uses both PeriodicTask and ResourceIntensiveTask
if (task is PeriodicTask)
{

    ShellTile TileToFind = ShellTile.ActiveTiles.First();


    if (TileToFind != null)
    {
        StandardTileData NewTileData = new StandardTileData
        {
            BackgroundImage = new Uri("isostore:Shared/ShellContent/tile.png", UriKind.Absolute),
            Title = strTitle,
            Count = null,
            BackTitle = (string)settings["SelectedCityName"],
            BackBackgroundImage = new Uri(strBackBackground, UriKind.Relative),
            BackContent = strWind + Environment.NewLine + strPrecipitation
        };

        TileToFind.Update(NewTileData);
    }
}
4

4 回答 4

1

有一种方法可以避免你的问题。

用户正在使用“返回”按钮关闭应用程序,对吧?所以你可以利用这一点。

只需向此事件添加方法,它必须在应用程序关闭之前完全执行。在您的 MainPage() 方法类型中:

this.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>((sender, e) =>
{
    blablablablabla - your code;
});
于 2012-05-04T17:58:29.057 回答
0

尝试在 Application_Launching 更新磁贴。

如果在Application_Activated处更新,在开始菜单和主全景画面之间快速切换肯定会出现黑屏。在 Application_Closing/Application_Deactivated 更新它也不起作用

于 2012-03-16T08:20:51.713 回答
0

当您的应用程序被停用时,您必须在多长时间内完成处理,没有硬性指导方针。如果您查看技术认证要求的第5.2节,它给出了一些关于退出/重新启动应用程序时响应时间限制的场景(必须在 5 秒内启动,在 20 秒内可用)。不遵守这些要求可能会阻止您的应用程序获得认证。还有一个看门狗进程,如果它在特定时间段内没有关闭,它将终止您的代码(抱歉,无法在此处找到确切的数字)。如果您故意在启动页面的构造函数中放置一个长时间运行的任务(甚至是一个),您可以看到这一点。Thread.Sleep()

如果您在本地创建图像(即没有网络调用),那么除非它是一个复杂的图像,否则您应该有足够的时间来做您所追求的。我的应用程序中有类似的要求,但它需要网络调用来获取图像。结果,我在应用程序启动时启动了更新任务,因此当应用程序加载时,我可以开始获取图像并在用户被我的应用程序分心时静默更新它。

如果您立即退出,您的图像最终会变成黑色 - 这是在您的启动画面仍在显示的时候吗?我相信,如果您的第一页的构造函数尚未显示,则看门狗的行为会略有不同。

于 2012-03-08T11:09:10.430 回答
0

作为一般规则,退出应用程序时执行操作是一个坏主意。您是否不能在应用程序的一般操作中找到另一个点(或多个点)来代替此操作?

您还没有说明如何更新磁贴,但如果您自己下载图像,则很可能在退出应用程序时无法执行此操作。此外,在您等待响应时,获取图像的异步请求不会阻止应用程序的关闭,因此该请求很可能会被中止。在等待响应时尝试阻止应用程序的关闭将是一个非常糟糕的主意,如果花费超过几秒钟可能会导致您的应用程序进程被操作系统杀死。

于 2012-03-08T11:18:48.873 回答