2013-08-20 112 views
1

我在IsolatedStorage中有一個圖像,並且我想以編程方式將其設置爲設備鎖定屏幕背景。我的問題是我無法獲得LockScreen.SetImageUri所需的正確路徑。從參考http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj206968(v=vs.105).aspx可以明顯看出``ms-appdata:/// local /「是本地圖像所需的前兆。如何使用LockScreen.SetImageUri設置Windows Phone 8鎖定屏幕背景

var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/"; 
var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute); 

我已經在我的應用程序IsolatedStorage稱爲Pictures其中JPG圖像從CameraCaptureTask保存創建的文件夾。我曾嘗試幾種方法通過上述方案訪問此文件夾中的圖片,但我總是在下一行

Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri); 

調試時,然而,我看到uri = "ms-appdata:///Local/Pictures/WP_20130812_001.jpg",這是怎麼不正確收到ArgumentException

我實現如下

private void recent_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{    
    capturedPicture = (sender as LongListSelector).SelectedItem as CapturedPicture; 

    if (capturedPicture != null) 
    { 
     //filename is the name of the image in the IsolatedStorage folder named Pictures 
     fileName = capturedPicture.FileName; 
    } 
} 

void setAsLockScreenMenuItem_Click(object sender, EventArgs e) 
{ 
    if (!String.IsNullOrEmpty(fileName)) 
    { 
     //PictureRepository.IsolatedStoragePath is a string = "Pictures"     
     //LockHelper("isostore:/" + PictureRepository.IsolatedStoragePath + "/" + fileName, false); //results in FileNotFoundException 
     LockHelper(PictureRepository.IsolatedStoragePath + "/" + fileName, false); //results in ArgumentException 
    } 
    else 
    { 
     MessageBoxResult result = MessageBox.Show("You must select an image to set it as your lock screen.", "Notice", MessageBoxButton.OK); 
     if (result == MessageBoxResult.OK) 
     { 
      return; 
     } 
    } 
} 

private async void LockHelper(string filePathOfTheImage, bool isAppResource) 
{ 
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. 

     // The following code example shows the new URI schema. 
     // ms-appdata points to the root of the local app data folder. 
     // ms-appx points to the Local app install folder, to reference resources bundled in the XAP package. 
     var schema = isAppResource ? "ms-appx:///" : "ms-appdata:///Local/"; 
     var uri = new Uri(schema + filePathOfTheImage, UriKind.Absolute); 

     // Set the lock screen background image. 
     Windows.Phone.System.UserProfile.LockScreen.SetImageUri(uri); 

     // Get the URI of the lock screen background image. 
     var currentImage = Windows.Phone.System.UserProfile.LockScreen.GetImageUri(); 
     System.Diagnostics.Debug.WriteLine("The new lock screen background image is set to {0}", currentImage.ToString()); 
    } 
    else 
    { 
     MessageBox.Show("You said no, so I can't update your background."); 
    } 
} 
catch (System.Exception ex) 
{ 
    System.Diagnostics.Debug.WriteLine(ex.ToString()); 
} 

}

我怎麼可能會修改LockScreen.SetImageUri到正確的預期URI?

回答

1

要從應用程序設置鎖定屏幕圖像,您可能需要將您的應用程序聲明爲鎖定屏幕提供程序。你可以做到這一點通過添加以下代碼修改WMAppManifest.xml文件:

<Extensions> 
    <Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" /> 
</Extensions> 

檢查是否你在你的清單文件這個標籤。

+0

謝謝,我已經這麼做了。 – Matthew

+0

注意到,我放' ..'閉合'Token'標籤像這樣 ' <擴展EXTENSIONNAME = 「LockScreen_Background」 ConsumerID =下 「{111DFF24-AA15-4A96-8006-2BFF8122084F}」 TaskID =「_ default」/> ' – Matthew

0

我希望這可以幫助您解決您的問題。

如果您的應用程序已安裝,請確保它是背景圖片提供程序。如果沒有,則進入設置 - >鎖定屏幕 - >背景並從列表中選擇您的應用程序。

在計劃方面:

1.聲明該應用程序的應用程序清單文件 編輯WMAppManifest.xml與XML編輯器的意圖,請確保以下擴展存在:

<Extensions> <Extension ExtensionName="LockScreen_Background" ConsumerID="{111DFF24-AA15-4A96-8006-2BFF8122084F}" TaskID="_default" /> </Extensions> 

2.編寫代碼來更改背景圖像 以下是如何編寫用於設置背景的代碼的示例。

private async void lockHelper(Uri backgroundImageUri, string backgroundAction) 
     { 
      try 
      { 
       //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 LockScreenManager.RequestAccessAsync(); 
       //Check the status to make sure we were given permission. 
       bool isProvider = LockScreenManager.IsProvidedByCurrentApplication; if (isProvider) 
       { 
        //Do the update. 
        Windows.Phone.System.UserProfile.LockScreen.SetImageUri(backgroundImageUri); 
        System.Diagnostics.Debug.WriteLine("New current image set to {0}", backgroundImageUri.ToString()); 
       } 
       else { MessageBox.Show("You said no, so I can't update your background."); } 
      } 
      catch (System.Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } 
     } 

關於尤里斯,有很多選擇,但要在頭腦:

要使用您在您的應用程序附帶的圖像,使用MS-APPX:///

Uri imageUri = new Uri("ms-appx:///background1.png", UriKind.RelativeOrAbsolute); LockScreen.SetImageUri(imageUri); 

要使用存儲在本地文件夾中的映像,請使用ms-appdata:/// local/shared/shellcontent 必須在之內或之下/ shared/shell內容子文件夾

Uri imageUri = new Uri("ms-appdata:///local/shared/shellcontent/background2.png", UriKind.RelativeOrAbsolute); LockScreen.SetImageUri(imageUri); 
+0

您還可以查看http://developer.nokia.com/Community/Wiki/Dynamic_Lock_Screen_for_Windows_Phone_8以獲取正在運行的示例 – hmadrigal

相關問題