BaconographyW8.PlatformServices.LiveTileService.CreateTile C# (CSharp) Метод

CreateTile() приватный Метод

private CreateTile ( string text, Uri smallImage, Uri largeImage ) : System.Threading.Tasks.Task
text string
smallImage System.Uri
largeImage System.Uri
Результат System.Threading.Tasks.Task
        private async Task CreateTile(string text, Uri smallImage, Uri largeImage)
        {
            text = text.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");

            bool textIsLong = text.Length > 42;
            bool largeImageIsTall = false;
            bool largeImageIsWide = false;
            bool smallImageIsTall = false;
            bool smallImageIsWide = false;

            StorageFile largeImageFile = null;
            StorageFile smallImageFile = null;

            if (largeImage != null)
            {
                largeImageFile = (await _imagesService.SaveFileFromUriAsync(largeImage, largeImage.LocalPath, "liveTiles")) as StorageFile;

                if(largeImageFile != null)
                {
                    var imageProperties = await largeImageFile.Properties.GetImagePropertiesAsync();
                    var imageRatio = ((double)imageProperties.Width / (double)imageProperties.Height);
                    largeImageIsTall = imageRatio < .9;
                    largeImageIsWide = imageRatio > 1.34;
                }
            }

            if (smallImage != null)
            {
                smallImageFile = (await _imagesService.SaveFileFromUriAsync(smallImage, smallImage.LocalPath, "liveTiles")) as StorageFile;

                if (smallImageFile != null)
                {
                    smallImageFile = (await _imagesService.SaveFileFromUriAsync(smallImage, smallImage.LocalPath, "liveTiles")) as StorageFile;
                    var imageProperties = await smallImageFile.Properties.GetImagePropertiesAsync();
                    var smallImageRatio = ((double)imageProperties.Width / (double)imageProperties.Height);
                    smallImageIsTall = smallImageRatio < .9;
                    smallImageIsWide = smallImageRatio > 1.34;
                }
            }

            string tileXmlString = null;

            //small & large image, text isnt long, large image is wide small image is neither tall nor wide: TileWidePeekImage06
            if (smallImageFile != null && largeImageFile != null && !textIsLong && largeImageIsWide && !smallImageIsTall && !smallImageIsWide)
            {
                var tileFormat = @"
                <tile>
                  <visual>
                    <binding template='TileWidePeekImage06'>
                      <image id='1' src='{0}' alt='{2}'/>
                      <image id='2' src='{1}' alt='{2}'/>
                      <text id='1'>{2}</text>
                    </binding>
                    <binding template='TileSquarePeekImageAndText04'>
                      <image id='1' src='{3}' alt='{2}'/>
                      <text id='1'>{2}</text>
                    </binding> 
                  </visual>
                </tile>";

                var sizedWideLargeImage = await _imagesService.GenerateResizedImage(largeImageFile, 310, 150, 0, 0, true) as StorageFile;
                var sizedSquareLargeImage = await _imagesService.GenerateResizedImage(largeImageFile, 150, 150, 0, 0, true) as StorageFile;

                if (sizedWideLargeImage == null || sizedSquareLargeImage == null)
                    return;

                var sizedWideLargeImagePath = "ms-appdata:///local/liveTiles/" + sizedWideLargeImage.DisplayName;
                var sizedSquareImagePath = "ms-appdata:///local/liveTiles/" + sizedSquareLargeImage.DisplayName;


                tileXmlString = string.Format(tileFormat, sizedWideLargeImagePath, sizedSquareImagePath, text, sizedSquareImagePath);
            }
            //large image: TileWidePeekImageAndText01
            else if (largeImageFile != null && largeImageIsWide)
            {
                var tileFormat = @"
                <tile>
                  <visual>
                    <binding template='TileWidePeekImageAndText01'>
                      <image id='1' src='{0}' alt='{1}'/>
                      <text id='1'>{1}</text>
                    </binding>
                    <binding template='TileSquarePeekImageAndText04'>
                      <image id='1' src='{2}' alt='{1}'/>
                      <text id='1'>{1}</text>
                    </binding>   
                  </visual>
                </tile>";

                var sizedWideLargeImage = await _imagesService.GenerateResizedImage(largeImageFile, 310, 150, 0, 0, true) as StorageFile;
                var sizedSquareLargeImage = await _imagesService.GenerateResizedImage(largeImageFile, 150, 150, 0, 0, true) as StorageFile;

                if (sizedWideLargeImage == null || sizedSquareLargeImage == null)
                    return;

                var sizedWideLargeImagePath = "ms-appdata:///local/liveTiles/" + sizedWideLargeImage.DisplayName;
                var sizedSquareImagePath = "ms-appdata:///local/liveTiles/" + sizedSquareLargeImage.DisplayName;

                tileXmlString = string.Format(tileFormat, sizedWideLargeImagePath, text, sizedSquareImagePath);
            }

            //small image only text is long: TileWideSmallImageAndText03
            else if (smallImageFile != null && textIsLong)
            {
                var tileFormat = @"
                <tile>
                  <visual>
                    <binding template='TileWideSmallImageAndText03'>
                      <image id='1' src='{0}' alt='{1}'/>
                      <text id='1'>{1}</text>
                    </binding>
                    <binding template='TileSquarePeekImageAndText04'>
                      <image id='1' src='{0}' alt='{1}'/>
                      <text id='1'>{1}</text>
                    </binding>
                  </visual>
                </tile>";

                var smallImageFilePath = "ms-appdata:///local/liveTiles/" + smallImageFile.DisplayName;

                tileXmlString = string.Format(tileFormat, smallImageFilePath, text);
            }

            //small image only text is short: TileWideSmallImageAndText01
            else if (smallImageFile != null && !textIsLong)
            {
                var tileFormat = @"
                <tile>
                  <visual>
                    <binding template='TileWideSmallImageAndText01'>
                      <image id='1' src='{0}' alt='{1}'/>
                      <text id='1'>{1}</text>
                    </binding>
                    <binding template='TileSquarePeekImageAndText04'>
                      <image id='1' src='{0}' alt='{1}'/>
                      <text id='1'>{1}</text>
                    </binding>
                  </visual>
                </tile>";

                var smallImageFilePath = "ms-appdata:///local/liveTiles/" + smallImageFile.DisplayName;

                tileXmlString = string.Format(tileFormat, smallImageFilePath, text);
            }

            //no image, text is long: TileWideText04
            else if (textIsLong)
            {
                var tileFormat = @"
                <tile>
                  <visual>
                    <binding template='TileWideText04'>
                      <text id='1'>{0}</text>
                    </binding>  
                    <binding template='TileSquareText04'>
                        <text id='1'>{0}</text>
                    </binding> 
                  </visual>
                </tile>";

                tileXmlString = string.Format(tileFormat, text);
            }

            //no image text is short: TileWideText03
            else
            {
                var tileFormat = @"
                <tile>
                  <visual>
                    <binding template='TileWideText03'>
                      <text id='1'>{0}</text>
                    </binding>
                    <binding template='TileSquareText04'>
                        <text id='1'>{0}</text>
                    </binding>  
                  </visual>
                </tile>";

                tileXmlString = string.Format(tileFormat, text);
            }
            // Generate the final XML obj
            XmlDocument final = new XmlDocument();
            final.LoadXml(tileXmlString);

            // Create and send the tile notification
            TileNotification notification = new TileNotification(final);
            _tileUpdater.Update(notification);
        }