IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    秒表+应用分享:自定义背景

    编程小梦发表于 2014-10-08 15:44:24
    love 0

    在“秒表+”上线之后很多人都建议支持自定义背景,这个确实有点儿难度,最后终于在小梦的帮助下完成了。以下介绍如何从图片库读取图片并保存到本地,并在程序启动时自动加载背景。

    首先在你的页面的类下添加一个变量

    FileOpenPickerContinuationEventArgs _filePickerEventArgs = null;

    然后再写这个么个函数

    public FileOpenPickerContinuationEventArgs FilePickerEvent

    {

    get { return _filePickerEventArgs; }

    set

    {

    _filePickerEventArgs = value;

    ContinueFileOpenPicker(_filePickerEventArgs);

    }

    }

    这个复制粘贴就好

    然后在这里用户点了某个按钮想要更换背景图片

    处理如下

    private void changeback(object sender, RoutedEventArgs e)

    {

    FileOpenPicker openPicker = new FileOpenPicker();

    openPicker.ViewMode = PickerViewMode.Thumbnail;

    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

    openPicker.FileTypeFilter.Add(“.jpg”);

    openPicker.FileTypeFilter.Add(“.jpeg”);

    openPicker.FileTypeFilter.Add(“.png”);

    openPicker.PickSingleFileAndContinue();

    }

    用户打了√之后程序将会执行到app.xaml.cs里,在这里添加这样一个函数,如果有,就改装吧

    protected override void OnActivated(IActivatedEventArgs args)

    {

    if (args is FileOpenPickerContinuationEventArgs)

    {

    Frame rootFrame = Window.Current.Content as Frame;

    if (!rootFrame.Navigate(typeof(normal )))//这个normal是你刚才点按钮的页面的类名

    throw new Exception(“Failed to create target page”);

    var p = rootFrame.Content as normal ;

    p.FilePickerEvent = (FileOpenPickerContinuationEventArgs)args;

    }

    Window.Current.Activate();

    }

    回到刚才的页面之后,程序将会执行到这里 这个函数也可以复制粘贴,只是这里有些内容你可能需要读懂才能用。

    public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)

    {

    if (  args.Files != null && args.Files.Count > 0)

    {

    StorageFile file = args.Files[0];

    IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

    BitmapImage bitmapImage = new BitmapImage();

    bitmapImage.SetSource(fileStream);

    ImageBrush newback = new ImageBrush();

    newback.ImageSource = bitmapImage;

    页背景[3] = newback;

    pageback = 页背景[3];

    applycolor();

    WriteFile(“changedbacksetting”, ”yes”);

    StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

    StorageFile storageFile = await applicationFolder.CreateFileAsync(@”userback.jpg”, CreationCollisionOption.ReplaceExisting);

    IRandomAccessStream outStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite);

    outStream.Size = 0;

    IRandomAccessStream inStream = fileStream;//供移植用

    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(inStream);

    PixelDataProvider provider = await decoder.GetPixelDataAsync();

    byte[] data = provider.DetachPixelData();

    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outStream);

    encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, decoder.PixelWidth, decoder.PixelHeight, decoder.DpiX, decoder.DpiY, data);

    try

    {

    await encoder.FlushAsync();

    }

    catch (Exception err)

    {

    Debug.WriteLine(err.ToString());

    }

    finally

    {

    inStream.Dispose();

    outStream.Dispose();

    }

    }

    }

    在刚才的函数中我们把目标图像设置成了背景图片,并将它保存到了本地,下面就是如何在程序启动的时候调取本地图像

    public async void ReadImg()

    {

    try

    {

    string fileName = @”userback.jpg”;

    StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

    StorageFile storageFile = await applicationFolder.GetFileAsync(fileName);

    IRandomAccessStream accessStream = await storageFile.OpenReadAsync();

    BitmapImage bitmapImage = new BitmapImage();

    bitmapImage.SetSource(accessStream);

    ImageBrush back = new ImageBrush();

    back.ImageSource = bitmapImage;

    normal.userback = back;

    accessStream.Dispose();

    }

    catch (Exception ex)

    {

    string x = ex.ToString();

    }

    }

    这个函数一定得在页面类的构造函数中调用,不然时间来不及。这么写一般还是来得及的,反正我写的应用还没在这方面出过问题,放心的用吧。

    最后附送几个uri的操作吧,很实用的。

    await Windows.System.Launcher.LaunchUriAsync(new Uri(“ms-windows-store:navigate?appid=” + ”f561757a-e831-4e06-95e9-458501e4d160″));//让用户访问某一个特定应用的页面,引导下载

    await Windows.System.Launcher.LaunchUriAsync(new Uri(“ms-windows-store:reviewapp?appid=” + Windows.ApplicationModel.Store.CurrentApp.AppId));//让用户评价本应用

    await Windows.System.Launcher.LaunchUriAsync(new Uri(“mailto:[imwangxiaoxin@live.com]“));给你发邮件反馈

    这些都是异步的操作,别忘了给本函数加上async标记



沪ICP备19023445号-2号
友情链接