我试图通过我的应用程序分享图像,当我通过visual studio在手机上运行应用程序时,一切正常,但是当我尝试从手机上运行它时,每次点击时它都会崩溃分享按钮
private async void dataTransferManager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
args.Request.Data.Properties.Title = "Let's Celebrate";
args.Request.Data.Properties.Description = "It's time to celebrate!";
DataRequestDeferral deferral = args.Request.GetDeferral();
try
{
var finalImg = await GenerateImage();
var folder = Package.Current.InstalledLocation;
const CreationCollisionOption option = CreationCollisionOption.ReplaceExisting;
var file = await folder.CreateFileAsync("letscelebrateshare.png", option);
var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
var pixelBuffer = await finalImg.GetPixelsAsync();
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
(uint)finalImg.PixelWidth,
(uint)finalImg.PixelHeight,
logicalDpi,
logicalDpi,
pixelBuffer.ToArray());
await encoder.FlushAsync();
StorageFile logoFile =
await Package.Current.InstalledLocation.GetFileAsync("letscelebrateshare.png");
List<IStorageItem> storageItems = new List<IStorageItem>();
storageItems.Add(logoFile);
args.Request.Data.SetStorageItems(storageItems);
}
}
finally
{
deferral.Complete();
}
}
答案 0 :(得分:0)
private async void bla... [..]
{
Exception exc= null;
try
{
//All your stuff
}
catch(Exception ex)
{
exc = ex;
}
if(exc!=null)
await msg.ShowAsync();
}
编辑:因为我不在C#中使用WP。我想你可以试试这个。希望这有助于^^
答案 1 :(得分:0)
打开文件打开选择器时遇到了同样的问题。我找到了一个奇怪的解决方案。当我删除每个页面中的导航参数并使用静态变量时,对我来说工作正常。我想这会对你有帮助
答案 2 :(得分:0)
如果应用程序在没有调试器的情况下崩溃,请在调试时尝试并且共享目标对于&#34;暂停和关闭&#34; (&#34;生命周期事件&#34;在visual studio中)。在大多数情况下,您的应用程序将崩溃,因为它已暂停并序列化数据。