0
我想以編程方式設置我的手機的鈴聲。 通過一些搜索,我發現這個代碼WP8.1 SaveRingtoneTask InvalidOperationException
private void RingToneSave()
{
SaveRingtoneTask ringtoneTask = new SaveRingtoneTask();
ringtoneTask.Completed += saveRingtoneChooser_Completed;
ringtoneTask.Source = new Uri(@"C:\Data\Programs\{9519D660-4D38-497F-9584-6497FF78C693}\Install\Craig David.wma");
ringtoneTask.DisplayName = "Ringtone";
ringtoneTask.Show();
}
然而ringtoneTask.Show();
拋出System.InvalidOperationException
以下是完整的詳細的異常:
An exception of type 'System.InvalidOperationException' occurred in Microsoft.Phone.ni.dll but was not handled in user code
Additional information: Path must point to a file in your Isolated Storage or Application Data directory.
然而,路徑指向我的獨立存儲一個文件,因爲我以前曾下載並將該文件保存到我的隔離存儲中。 我還使用IsoStorySpy(一種檢查手機的隔離存儲的工具)來確保文件位於獨立存儲中。
我理解錯了嗎?有沒有另一種方法來設置我的手機的鈴聲,而不使用SaveRingtoneTask
?
更新
private void RingToneSave(Uri sURI)
{
SaveRingtoneTask ringtoneTask = new SaveRingtoneTask();
ringtoneTask.Completed += saveRingtoneChooser_Completed;
ringtoneTask.Source = sURI;
ringtoneTask.DisplayName = "Ringtone";
ringtoneTask.Show();
}
public async Task<Problem> DownloadFileFromWeb(Uri uriToDownload, string fileName, CancellationToken cToken)
{
try
{
using (Stream mystr = await DownloadFile(uriToDownload))
using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
{
if (ISF.FileExists(fileName))
{
ISF.DeleteFile(fileName);
}
using (IsolatedStorageFileStream file = ISF.CreateFile(fileName))
{
const int BUFFER_SIZE = 8192;
byte[] buf = new byte[BUFFER_SIZE];
int bytesread = 0;
while ((bytesread = await mystr.ReadAsync(buf, 0, BUFFER_SIZE)) > 0)
{
double percentage = ((double)file.Length/(double)mystr.Length) * (double)100;
textBlock.Text = Math.Round(percentage).ToString() + "%";
cToken.ThrowIfCancellationRequested();
file.Write(buf, 0, bytesread);
}
sRingTonePath = file.Name;
}
}
RingToneSave(new Uri(sRingTonePath));
return Problem.Ok;
}
catch (Exception exc)
{
if (exc is OperationCanceledException)
return Problem.Cancelled;
else
{
MessageBox.Show(exc.Message);
return Problem.Other;
}
}
}
我想是這樣的sRingTonePath = Path.GetFullPath(ISF.GetFileNames()[0 ]);然後使用sRingTonePath作爲源,但仍然是同樣的問題。 – user2530266
我以前做過這些,爲了創建應用程序,我需要它,我遵循的步驟是 1)使用HttpClient將文件下載到Stream對象中 2)在隔離存儲(Local Storage)中創建一個新文件)並將流保存在其中 3)開始一個新的RingToneTask並將Source屬性設置爲之前創建的文件。完成! –
我正在做同樣的事情。我更新了這個問題。你是這個意思嗎? – user2530266