private void HomeContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
 {
     HomeContentDialog.Hide();
     //Frame.GoBack();
     App.Model.StartVisibility = Visibility.Visible;
 }
 private void HomeContentDialog_CloseButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
 {
     HomeContentDialog.Hide();
 }
        private async void HomeContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            HomeContentDialog.Hide();
            LoadingControl.IsLoading = true;//Con:加载控件
            await Task.Delay(400);

            //1、创建一个XDocument对象
            XDocument    xDoc = new XDocument();
            XDeclaration XDec = new XDeclaration("1.0", "utf-8", "no");

            //设置xml的文档定义
            xDoc.Declaration = XDec;


            //2、创建根节点
            XElement root = new XElement("Layers");

            xDoc.Add(root);

            XElement Width = new XElement("Width", App.Model.Width);

            root.Add(Width);
            XElement Height = new XElement("Height", App.Model.Height);

            root.Add(Height);
            XElement Tool = new XElement("Tool", App.Model.Tool);

            root.Add(Tool);
            XElement Index = new XElement("Index", App.Model.Index);

            root.Add(Index);


            //3、创建主图片
            ICanvasImage ci = App.Model.NullRenderTarget;

            for (int i = App.Model.Layers.Count - 1; i >= 0; i--)  //自下而上渲染
            {
                ci = App.RenderTransform(App.Model.Layers[i], ci); //渲染
            }
            using (CanvasDrawingSession ds = App.Model.SecondBottomRenderTarget.CreateDrawingSession())
            {
                ds.Clear(Color.FromArgb(0, 0, 0, 0));
                ds.DrawImage(ci);
            }
            var      MainBytes        = App.Model.SecondBottomRenderTarget.GetPixelBytes(); //把位图转为byte[]
            var      MainString       = Convert.ToBase64String(MainBytes);                  //把btye[]转为字符串
            XElement MainCanvasRender = new XElement("MainRenderTarget", MainString);       //字符串写入xml节点

            root.Add(MainCanvasRender);


            //4、循环创建节点
            foreach (var l in App.Model.Layers)
            {
                XElement Layer = new XElement("Layer");
                root.Add(Layer);

                //4、创建元素
                XElement LayerName = new XElement("LayerName", l.Name);
                Layer.Add(LayerName);
                XElement LayerVisual = new XElement("LayerVisual", l.Visual);
                Layer.Add(LayerVisual);
                XElement LayerOpacity = new XElement("LayerOpacity", l.Opacity);
                Layer.Add(LayerOpacity);
                XElement LayerBlendIndex = new XElement("LayerBlendIndex", l.BlendIndex);
                Layer.Add(LayerBlendIndex);

                XElement LayerWidth = new XElement("LayerWidth", l.CanvasRenderTarget.SizeInPixels.Width);
                Layer.Add(LayerWidth);
                XElement LayerHeight = new XElement("LayerHeight", l.CanvasRenderTarget.SizeInPixels.Height);
                Layer.Add(LayerHeight);

                var      Bytes = l.CanvasRenderTarget.GetPixelBytes();                 //把位图转为byte[]
                var      str   = Convert.ToBase64String(Bytes);                        //把btye[]转为字符串
                XElement CanvasRenderTarget = new XElement("CanvasRenderTarget", str); //字符串写入xml节点
                Layer.Add(CanvasRenderTarget);
            }


            //5、保存
            string path = ApplicationData.Current.LocalFolder.Path + "/" + App.Model.Name + ".photo"; //将XML文件加载进来

            xDoc.Save(path);


            //6、缩略图 (裁切成宽高最大256的图片)


            //缩略图缩放比例
            float scale = App.Model.Width < App.Model.Height ? 256.0f / App.Model.Width : 256.0f / App.Model.Height;

            //缩放后宽高并确定左右上下偏移
            float W = scale * App.Model.Width;
            float H = scale * App.Model.Height;

            CanvasRenderTarget crt = new CanvasRenderTarget(App.Model.VirtualControl, W, H);

            using (CanvasDrawingSession ds = crt.CreateDrawingSession())
            {
                //绘制缩略图
                ds.DrawImage(new ScaleEffect
                {
                    Source = App.Model.MainRenderTarget,
                    Scale  = new Vector2(scale)
                });
            }
            Library.Image.SavePng(ApplicationData.Current.LocalFolder, crt, App.Model.Name, CreationCollisionOption.ReplaceExisting);

            LoadingControl.IsLoading = false;//Con:加载控件

            // Frame.GoBack();
            App.Model.StartVisibility = Visibility.Visible;
        }