0

そのため、FileSavePicker は空のファイルを作成するだけであり、実際にファイルに書き込むには追加のコードを記述する必要があると私は言いました。FileSavePicker の後に Task WriteToFile を開始しましたが、終了方法がわかりません。FileSavePicker を使用して、ユーザーはファイルを保存するフォルダーを選択します。WriteToFile コードのどこにそれを指定すればよいのでしょうか? また、ファイル ソースを正確にどのように配置すればよいでしょうか? 保存するファイルはすべてアプリに同梱されています。ここでは例として x.mp3 を使用しています。

    public class SoundData : ViewModelBase
    {
        public string Title { get; set; }
        public string FilePath { get; set; }



        public RelayCommand<string> SaveSoundAs { get; set; }

        private async void ExecuteSaveSoundAs(string soundPath)
        {

        string path = @"appdata:/x.mp3";
        StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
        StorageFile file = await folder.GetFileAsync(path);



                {
                    FileSavePicker savePicker = new FileSavePicker();
                    savePicker.SuggestedSaveFile = file;
                    savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
                    savePicker.ContinuationData.Add("SourceSound", soundPath);
                    savePicker.SuggestedFileName = this.Title;
                    savePicker.PickSaveFileAndContinue();

                }

        }

        public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
        {
            string soundPath = (string)args.ContinuationData["SourceSound"];
            StorageFile file = args.File;
            if (file != null)
            {
                // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
                CachedFileManager.DeferUpdates(file);
                // write to file



                await FileIO.WriteTextAsync(file, file.Name);
                // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
                // Completing updates may require Windows to ask for user input.
                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
                if (status == FileUpdateStatus.Complete) ;

            }
        }



        public SoundData()
        {
            SaveSoundAs = new RelayCommand<string>(ExecuteSaveSoundAs);
        }







    }
}
4

1 に答える 1

1

Windows Phone の場合は、このHow to documentationに従ってください。ワークフローは、Silverlight アプリからかなり変更されました。古いタスクで使用されていたように、アプリが再開されなくなります。

ドキュメントのすべての手順に従う必要はありませんが、OnActivatedApp.xaml.cs 内のメソッドを実行することが重要です。その中で、ContinueFileSavePicker メソッドを呼び出します。

これは、ダウンロードできるサンプルであり、役立つはずです。

アップデート

アプリと共に出荷するファイルを保存する場合は、次のコードを試してピッカーを初期化してください

// Get the local file that is shipped with the app
// file but be "content" and not "resource"
string path = @"Assets\Audio\Sound.mp3";
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await folder.GetFileAsync(path);

// Show the picker
FileSavePicker savePicker = new FileSavePicker();
// Set the file that will be saved
savePicker.SuggestedSaveFile = file;
savePicker.SuggestedFileName = "Sound";
savePicker.FileTypeChoices.Add("MP3", new List<string>() { ".mp3" });
savePicker.PickSaveFileAndContinue();

また、PhoneApplicationService の ContractActivated イベントを確実にリッスンする必要があります。このイベントは、アプリがピッカーから戻ったときに (8.1 Silverlight アプリで) 発生します。これは、ContinueFileSavePicker メソッドを呼び出す場所です。必要に応じて、いつでもロジックをそこに入れることができます。

xaml でイベントをサブスクライブします。

<Application.ApplicationLifetimeObjects>
    <!--Required object that handles lifetime events for the application-->
    <shell:PhoneApplicationService
        ContractActivated="Application_ContractActivated"
        Launching="Application_Launching" Closing="Application_Closing"
        Activated="Application_Activated" Deactivated="Application_Deactivated"/>
</Application.ApplicationLifetimeObjects>

そして App.xaml.cs で:

private async void Application_ContractActivated(object sender, Windows.ApplicationModel.Activation.IActivatedEventArgs e)
{
    var args = e as FileSavePickerContinuationEventArgs ;
    if (args != null)
    {
        StorageFile file = args.File; 
        if (file != null) 
        { 
            // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync. 
            CachedFileManager.DeferUpdates(file); 
            // write to file 
            await FileIO.WriteTextAsync(file, file.Name); 
            // Let Windows know that we're finished changing the file so the other app can update the remote version of the file. 
            // Completing updates may require Windows to ask for user input. 
            FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); 
        }
    }
}
于 2014-06-04T22:43:50.270 に答える