|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Windows.Foundation; |
| 4 | +using Windows.UI.StartScreen; |
| 5 | +using Windows.UI.Xaml; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +namespace Template10.Services.SecondaryTileService |
| 9 | +{ |
| 10 | + public class SecondaryTileHelper |
| 11 | + { |
| 12 | + public async Task<bool> PinAsync(TileInfo info, string tileId, string arguments) |
| 13 | + { |
| 14 | + System.Diagnostics.Contracts.Contract.Requires(info != null, "TileInfo"); |
| 15 | + System.Diagnostics.Contracts.Contract.Requires(!string.IsNullOrEmpty(tileId), "TileId"); |
| 16 | + |
| 17 | + if (Exists(tileId)) |
| 18 | + { |
| 19 | + var existings = await SecondaryTile.FindAllAsync(); |
| 20 | + var existing = existings.FirstOrDefault(x => x.Arguments.Equals(arguments)); |
| 21 | + } |
| 22 | + |
| 23 | + var tile = new SecondaryTile() |
| 24 | + { |
| 25 | + TileId = tileId, |
| 26 | + DisplayName = info.DisplayName, |
| 27 | + Arguments = arguments, |
| 28 | + PhoneticName = info.PhoneticName, |
| 29 | + LockScreenDisplayBadgeAndTileText = info.LockScreenDisplayBadgeAndTileText, |
| 30 | + }; |
| 31 | + |
| 32 | + if (info.LockScreenBadgeLogo != null) |
| 33 | + { |
| 34 | + tile.LockScreenBadgeLogo = info.LockScreenBadgeLogo; |
| 35 | + } |
| 36 | + |
| 37 | + tile.VisualElements.BackgroundColor = info.VisualElements.BackgroundColor; |
| 38 | + tile.VisualElements.ForegroundText = info.VisualElements.ForegroundText; |
| 39 | + tile.VisualElements.ShowNameOnSquare150x150Logo = info.VisualElements.ShowNameOnSquare150x150Logo; |
| 40 | + tile.VisualElements.ShowNameOnSquare310x310Logo = info.VisualElements.ShowNameOnSquare310x310Logo; |
| 41 | + tile.VisualElements.ShowNameOnWide310x150Logo = info.VisualElements.ShowNameOnWide310x150Logo; |
| 42 | + |
| 43 | + if (info.VisualElements.Square150x150Logo != null) |
| 44 | + { |
| 45 | + tile.VisualElements.Square150x150Logo = info.VisualElements.Square150x150Logo; |
| 46 | + } |
| 47 | + |
| 48 | + if (info.VisualElements.Square30x30Logo != null) |
| 49 | + { |
| 50 | + tile.VisualElements.Square30x30Logo = info.VisualElements.Square30x30Logo; |
| 51 | + } |
| 52 | + |
| 53 | + if (info.VisualElements.Square310x310Logo != null) |
| 54 | + { |
| 55 | + tile.VisualElements.Square310x310Logo = info.VisualElements.Square310x310Logo; |
| 56 | + } |
| 57 | + |
| 58 | + if (info.VisualElements.Wide310x150Logo != null) |
| 59 | + { |
| 60 | + tile.VisualElements.Wide310x150Logo = info.VisualElements.Wide310x150Logo; |
| 61 | + } |
| 62 | + |
| 63 | + var result = await tile.RequestCreateForSelectionAsync(info.Rect(), info.RequestPlacement); |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + public bool Exists(string tileId) |
| 68 | + { |
| 69 | + return SecondaryTile.Exists(tileId); |
| 70 | + } |
| 71 | + |
| 72 | + public async Task<bool> UnpinAsync(string tileId, Rect selection, Windows.UI.Popups.Placement placement = Windows.UI.Popups.Placement.Above) |
| 73 | + { |
| 74 | + System.Diagnostics.Contracts.Contract.Requires(tileId != null, "TileId"); |
| 75 | + |
| 76 | + if (!SecondaryTile.Exists(tileId)) |
| 77 | + return true; |
| 78 | + var tile = new SecondaryTile(tileId); |
| 79 | + var result = await tile.RequestDeleteForSelectionAsync(selection, placement); |
| 80 | + return result; |
| 81 | + } |
| 82 | + |
| 83 | + public class TileInfo |
| 84 | + { |
| 85 | + public FrameworkElement AnchorElement { get; set; } |
| 86 | + public Windows.UI.Popups.Placement RequestPlacement { get; set; } |
| 87 | + |
| 88 | + public string Arguments { get; set; } |
| 89 | + public string DisplayName { get; set; } |
| 90 | + public string PhoneticName { get; set; } |
| 91 | + public bool LockScreenDisplayBadgeAndTileText { get; set; } |
| 92 | + public Uri LockScreenBadgeLogo { get; set; } |
| 93 | + |
| 94 | + public TileVisualElements VisualElements = new TileVisualElements(); |
| 95 | + public class TileVisualElements |
| 96 | + { |
| 97 | + public Windows.UI.Color BackgroundColor { get; set; } |
| 98 | + public ForegroundText ForegroundText { get; set; } |
| 99 | + public bool ShowNameOnSquare150x150Logo { get; set; } |
| 100 | + public bool ShowNameOnSquare310x310Logo { get; set; } |
| 101 | + public bool ShowNameOnWide310x150Logo { get; set; } |
| 102 | + public Uri Square150x150Logo { get; set; } |
| 103 | + public Uri Square30x30Logo { get; set; } |
| 104 | + public Uri Square310x310Logo { get; set; } |
| 105 | + public Uri Wide310x150Logo { get; set; } |
| 106 | + } |
| 107 | + |
| 108 | + public Rect Rect() |
| 109 | + { |
| 110 | + if (this.AnchorElement == null) |
| 111 | + return new Rect(); |
| 112 | + var transform = this.AnchorElement.TransformToVisual(null); |
| 113 | + var point = transform.TransformPoint(new Point()); |
| 114 | + var size = new Size(this.AnchorElement.ActualWidth, this.AnchorElement.ActualHeight); |
| 115 | + return new Rect(point, size); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments