锁屏自定义图像在创建后的第一次更新时会损坏

时间:2014-10-13 23:44:50

标签: windows-phone-8 user-controls windows-phone lockscreen

我有一个Windows Phone 8.0应用程序,我正在开发其锁屏支持。

我已经为锁定屏幕图像创建了一个用户控件。第一次运行时,实现此功能的代码完美无瑕。也就是说,当我打开锁定屏幕时,会正确呈现锁定屏幕自定义图像。

enter image description here

但是,当锁定屏幕更新时,图像无法正确呈现,所有元素都在彼此之上。

enter image description here

这是创建锁定屏幕图像的代码,然后用于更新它:

    public async void CreateOrUpdateLockScreen()
{
    try
    {
        try
        {
            var isProvider = Windows.Phone.System.UserProfile.LockScreenManager.IsProvidedByCurrentApplication;
            if (!isProvider)
            {
                // If you're not the provider, this call will prompt the user for permission.
                // Calling RequestAccessAsync from a background agent is not allowed.
                var op = await Windows.Phone.System.UserProfile.LockScreenManager.RequestAccessAsync();

                // Only do further work if the access was granted.
                isProvider = op == Windows.Phone.System.UserProfile.LockScreenRequestResult.Granted;
            }

            if (isProvider)
            {
                // At this stage, the app is the active lock screen background provider.
                var image = new Weathercast.Core.Lockscreen_NowToday();
                image.Measure(new Size(720, 1280));
                image.Arrange(new Rect(0, 0, 720, 1280));

                var bmp = new WriteableBitmap(720, 1280);
                bmp.Render(image, null);

                bmp.Invalidate();

                string path = "/Shared/ShellContent/";
                string fileName;

                try
                {
                    var currentImage = Windows.Phone.System.UserProfile.LockScreen.GetImageUri();

                    if (currentImage.ToString().EndsWith("_A.jpg"))
                    {
                        fileName = "LockScreen_B.jpg";
                    }
                    else
                    {
                        fileName = "LockScreen_A.jpg";
                    }
                }
                catch (Exception e)
                {
                    fileName = "LockScreen_A.jpg";
                }


                using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (!isf.DirectoryExists("/LockScreen"))
                    {
                        isf.CreateDirectory("/LockScreen");
                    }

                    using (var stream = isf.OpenFile(path + fileName, System.IO.FileMode.OpenOrCreate))
                    {
                        bmp.SaveJpeg(stream, 720, 1280, 0, 100);
                    }
                }

                Uri uri = new Uri("ms-appdata:///Local" + path + fileName, UriKind.Absolute);

                // Set the lock screen background image.
                Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri);
            }
            else
            {
                MessageBox.Show("You said no, so I can't update your background.");
            }
        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }
    catch (Exception e)
    {

    }
}

更新:

这是我的用户控件的锁定,LockScreen_NowToday,锁定屏幕图像基于(注意:只显示整体结构以提供想法,如果需要更多详细信息,请告诉我):

<UserControl 
x:Class="Weathercast.Core.Lockscreen_NowToday"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilySemiBold}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="White">


<Border 
    x:Name="LayoutRoot"
    Height="1280" Width="720"
    Margin="0,100,0,0">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="3*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="19"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="19"/>
        </Grid.ColumnDefinitions>

    ..

    </Grid>

..
</Border>

LockScreen_NowToday.xaml.cs:

using System.ComponentModel;
using System.Windows.Controls;


public partial class Lockscreen_NowToday : UserControl, INotifyPropertyChanged
{
    /** PROPERTIES **/

    private string[] _temperature = new string[4];
    public string[] Temperature
    {
        get { return _temperature; }
        set
        {
            this._temperature = value;
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Temperature"));
            }
        }
    }

    /** CONSTRUCTOR **/

    public Lockscreen_NowToday()
    {
        InitializeComponent();
        this.DataContext = this;
    }


}

1 个答案:

答案 0 :(得分:1)

当文本聚集时,通常是您正在渲染的控件的布局有问题。尝试在测量和安排后调用UpdateLayout。

WriteableBitmap writeableBitmap = new WriteableBitmap(Width, Height);
weatherControl.Measure(new Size(Width, Height));
weatherControl.UpdateLayout();
weatherControl.Arrange(new Rect(0, 0, Width, Height));
weatherControl.UpdateLayout();

writeableBitmap.Render(tileBackground, null);
writeableBitmap.Invalidate();