- 浏览: 140036 次
文章分类
最新评论
win10 uwp 活动磁贴
本文翻译:https://mobileprogrammerblog.wordpress.com/2015/12/23/live-tiles-and-notifications-in-universal-windows-10-app/ 我会写很多质量很低文章,文章都是胡说,如果看不懂可以发到邮箱
动态磁贴是看起来很好看的东西,放在开始菜单,看来是下面图
win10总有很多看起来有用,但实际没什么卵用的东西,我一点不觉得用户觉得这个有用,但是我们能做活动磁贴UWP,微软一直把开发者当成用户。
做一个UWP当然需要我们打开神器
新建一个项目,空UWP,可以使用快捷键ctrl+shift+N
我们打开MainPage.xaml,新建的时候有点慢,我们需要等一下如果放在固态基本不用等。
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Margin="12">
<TextBlock Text="Adaptive Tiles" FontSize="20" FontWeight="Bold" />
<Button Click="UpdateBadge" VerticalAlignment="Top" Margin="12" Background="#330070B0">Update Badge Count</Button>
<Button Click="UpdatePrimaryTile" VerticalAlignment="Top" Background="#330070B0" Margin="12">Update Primary Tile</Button>
</StackPanel>
<StackPanel Grid.Row="1" Margin="12">
<TextBlock Text="Interactive Toast" FontSize="20" FontWeight="Bold" />
<StackPanel Orientation="Horizontal" Margin="12">
<TextBlock x:Name="Description" VerticalAlignment="Center" Text="{x:Bind CurrentToDoTask.Description, Mode=OneWay}" FontWeight="Bold" />
<CheckBox Margin="12,0,0,0" IsChecked="{x:Bind CurrentToDoTask.IsComplete, Mode=OneWay}" IsEnabled="False" />
</StackPanel>
<Button Click="Notify" Background="#330070B0" Margin="12">Notify</Button>
<Button Background="#330070B0" Click="{x:Bind Refresh}" Margin="12">Refresh</Button>
</StackPanel>
</Grid>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Margin="12">
<TextBlock Text="我翻译的 本文来自http://blog.csdn.net/lindexi_gd" />
<TextBlock Text="磁贴" FontSize="20" FontWeight="Bold" />
<Button Click="UpdateBadge" VerticalAlignment="Top" Margin="12" Background="#330070B0">更新磁贴数</Button>
<Button Click="UpdatePrimaryTile" VerticalAlignment="Top" Background="#330070B0" Margin="12">更新显示磁贴</Button>
</StackPanel>
<StackPanel Grid.Row="1" Margin="12">
<TextBlock Text="互动吐司" FontSize="20" FontWeight="Bold" />
<StackPanel Orientation="Horizontal" Margin="12">
<TextBlock x:Name="xdescription" VerticalAlignment="Center" Text="{x:Bind CurrentToDoTask.Description, Mode=OneWay}" FontWeight="Bold" />
<CheckBox Margin="12,0,0,0" IsChecked="{x:Bind CurrentToDoTask.IsComplete, Mode=OneWay}" IsEnabled="False" />
</StackPanel>
<Button Click="Notify" Background="#330070B0" Margin="12">通知</Button>
<Button Background="#330070B0" Click="{x:Bind Refresh}" Margin="12">更新</Button>
</StackPanel>
</Grid>
</Grid>
写完我们可以看到下面的样子
上面一张是作者写的开始我没有去看,以为他写出来就是上面那图,复制了他代码在我写博客,发现他的代码错了,我自己重新写,发现我应该弄个中文,就写了第二张图,我们看到上面代码是第二张图。
我们右击方案新建一个文件夹DATA
,里面新建一个类PrimaryTile
,可以看下面图
我们在PrimaryTile
public class PrimaryTile
{
public string time
{
set;
get;
} = "8:15 AM, Saturday";
public string message
{
set;
get;
} = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.";
public string message2
{
set;
get;
} = "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident.";
public string branding
{
set;
get;
} = "name";
public string appName
{
set;
get;
} = "UWP";
}
创建一个文件夹services
新建tileservice.cs
toastservice.cs
public class TileService
{
public static void SetBadgeCountOnTile(int count)
{
// Update the badge on the real tile
System.Xml.XmlDocument badgeXml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber);
XmlElement badgeElement = (XmlElement) badgeXml.SelectSingleNode("/badge");
badgeElement.SetAttribute("value", count.ToString());
BadgeNotification badge = new BadgeNotification(badgeXml);
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badge);
}
public static XmlDocument CreateTiles(PrimaryTile primaryTile)
{
XDocument xDoc = new XDocument(
new XElement("tile", new XAttribute("version", 3),
new XElement("visual",
// Small Tile
new XElement("binding", new XAttribute("branding", primaryTile.branding),
new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileSmall"),
new XElement("group",
new XElement("subgroup",
new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),
new XElement("text", primaryTile.message,
new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),
new XAttribute("hint-maxLines", 3))
)
)
),
// Medium Tile
new XElement("binding", new XAttribute("branding", primaryTile.branding),
new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileMedium"),
new XElement("group",
new XElement("subgroup",
new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),
new XElement("text", primaryTile.message,
new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),
new XAttribute("hint-maxLines", 3))
)
)
),
// Wide Tile
new XElement("binding", new XAttribute("branding", primaryTile.branding),
new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileWide"),
new XElement("group",
new XElement("subgroup",
new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),
new XElement("text", primaryTile.message,
new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),
new XAttribute("hint-maxLines", 3)),
new XElement("text", primaryTile.message2,
new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),
new XAttribute("hint-maxLines", 3))
),
new XElement("subgroup", new XAttribute("hint-weight", 15),
new XElement("image", new XAttribute("placement", "inline"),
new XAttribute("src", "Assets/StoreLogo.png"))
)
)
),
//Large Tile
new XElement("binding", new XAttribute("branding", primaryTile.branding),
new XAttribute("displayName", primaryTile.appName), new XAttribute("template", "TileLarge"),
new XElement("group",
new XElement("subgroup",
new XElement("text", primaryTile.time, new XAttribute("hint-style", "caption")),
new XElement("text", primaryTile.message,
new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),
new XAttribute("hint-maxLines", 3)),
new XElement("text", primaryTile.message2,
new XAttribute("hint-style", "captionsubtle"), new XAttribute("hint-wrap", true),
new XAttribute("hint-maxLines", 3))
),
new XElement("subgroup", new XAttribute("hint-weight", 15),
new XElement("image", new XAttribute("placement", "inline"),
new XAttribute("src", "Assets/StoreLogo.png"))
)
)
)
)
)
);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xDoc.ToString());
//Debug.WriteLine(xDoc);
return xmlDoc;
}
}
public static class ToastService
{
public static System.Xml.XmlDocument CreateToast()
{
XDocument xDoc = new XDocument(
new XElement("toast",
new XElement("visual",
new XElement("binding", new XAttribute("template", "ToastGeneric"),
new XElement("text", "To Do List"),
new XElement("text", "Is the task complete?")
) // binding
), // visual
new XElement("actions",
new XElement("action", new XAttribute("activationType", "background"),
new XAttribute("content", "Yes"), new XAttribute("arguments", "yes")),
new XElement("action", new XAttribute("activationType", "background"),
new XAttribute("content", "No"), new XAttribute("arguments", "no"))
) // actions
)
);
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(xDoc.ToString());
return xmlDoc;
}
}
我们创建文件ToDoTask.cs
ToDoTaskFileHelper.cs
public class ToDoTask
{
public string Description
{
get;
set;
}
public Guid Id
{
get;
set;
}
public bool? IsComplete
{
get;
set;
}
public string ToJson()
{
using (MemoryStream stream = new MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof (ToDoTask));
serializer.WriteObject(stream, this);
stream.Position = 0;
byte[] jsonBytes = stream.ToArray();
return Encoding.UTF8.GetString(jsonBytes, 0, jsonBytes.Length);
}
}
public static ToDoTask FromJson(string json)
{
// note: throws exception if the json is not valid
JsonObject jsonData = JsonObject.Parse(json);
// exceptions will be thrown if the values do not match the types
return new ToDoTask
{
Id = Guid.Parse(jsonData["Id"].GetString()),
Description = jsonData["Description"].GetString(),
IsComplete = jsonData["IsComplete"].GetBoolean()
};
}
}
public static class ToDoTaskFileHelper
{
public static async Task ReadToDoTaskJsonAsync()
{
// declare an empty variable to be filled later
string json = null;
// define where the file resides
StorageFolder localfolder = ApplicationData.Current.LocalFolder;
// see if the file exists
if (await localfolder.TryGetItemAsync(Filename) != null)
{
// open the file
StorageFile textfile = await localfolder.GetFileAsync(Filename);
// read the file
json = await FileIO.ReadTextAsync(textfile);
}
// if the file doesn't exist, we'll copy the app copy to local storage
else
{
StorageFile storageFile =
await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/task.json"));
await storageFile.CopyAsync(ApplicationData.Current.LocalFolder);
json = await FileIO.ReadTextAsync(storageFile);
}
return json;
}
public static async Task SaveToDoTaskJson(string json)
{
StorageFolder localfolder = ApplicationData.Current.LocalFolder;
StorageFile textfile = await localfolder.GetFileAsync(Filename);
await FileIO.WriteTextAsync(textfile, json);
}
private static readonly string Filename = "task.json";
}
task.json
{"Description":"A test task","Id":"9d6c3585-d0c2-4885-8fe0-f02727f8e483","IsComplete":true}
我们把刚才写的MainPage的按钮绑定到
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
public MainPage()
{
InitializeComponent();
Loaded += MainPage_Loaded;
}
#region Delegates
public event PropertyChangedEventHandler PropertyChanged;
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Refresh();
}
#endregion
public ToDoTask CurrentToDoTask
{
get
{
return _currentToDoTask;
}
set
{
_currentToDoTask = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CurrentToDoTask)));
}
}
private int _count;
private ToDoTask _currentToDoTask;
private async void Refresh()
{
void json = await ToDoTaskFileHelper.ReadToDoTaskJsonAsync();
CurrentToDoTask = ToDoTask.FromJson(json);
}
private void UpdateBadge(object sender, RoutedEventArgs e)
{
_count++;
TileService.SetBadgeCountOnTile(_count);
}
private void UpdatePrimaryTile(object sender, RoutedEventArgs e)
{
XmlDocument xmlDoc = TileService.CreateTiles(new PrimaryTile());
TileUpdater updater = TileUpdateManager.CreateTileUpdaterForApplication();
TileNotification notification = new TileNotification(xmlDoc);
updater.Update(notification);
}
private void Notify(object sender, RoutedEventArgs e)
{
System.Xml.XmlDocument xmlDoc = ToastService.CreateToast();
ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier();
ToastNotification toast = new ToastNotification(xmlDoc);
notifier.Show(toast);
}
}
写完自己运行就可以知道,更新磁贴,更新界面,提示通知,每个对应的代码自己可以看到,这个国内很多教程
相关推荐
- WinUI 是一套现代化的 UI 控件和 XAML 控件库,专为 Windows 10 设计,可用于 UWP 和 Win32 应用程序。 2. **MSIX:** - MSIX 是一种新的应用程序打包格式,支持传统 Win32 应用程序和 UWP 应用程序的安装和...
描述中提到的“gank.io首个win10客户端,支持磁贴、通知、小娜……已发布到应用商店,欢迎下载。”意味着这款应用不仅具有常规的客户端功能,还充分利用了UWP平台的特点。它支持Windows 10的动态磁贴,可以实时展示...
在UWP的世界里,开发者不仅可以利用丰富的API和库来实现各种功能,还能享受到Windows 10提供的特性,如触控支持、Continuum模式(适应不同设备的屏幕尺寸和输入方式)以及Live Tiles(动态磁贴)等。随着Windows生态...
Windows 10不仅支持传统桌面应用,还支持触摸屏和现代UWP(通用Windows平台)应用,为不同类型的设备提供一致的用户体验。此外,针对SSD硬盘的优化使得Win10在启动、关闭和应用响应速度上有显著提升,提升了整体...
标题 "开发:Windows 10 UWP无线标签/收据打印机演示" 提供的信息表明,这是一个关于使用Windows 10通用Windows平台(UWP)应用程序接口进行无线标签和收据打印的开发教程或示例。在Windows 10中,UWP允许开发者创建...
此外,Windows 10还支持开发UWP(通用Windows平台)应用,这些应用能够在各种Windows 10设备上运行,包括PC、平板和手机。 在系统管理和维护方面,两本书都会教导读者如何利用内置工具进行系统诊断、性能监控和故障...
以下内容将详细介绍Windows 10的通用Windows平台(UWP)的开发,包括但不限于XAML基础、布局设计、用户交互、样式、资源和主题、图形、变换和动画、数据绑定、导航和窗口管理以及动态磁贴和通知等方面的开发技术。...
【C# Win8皮肤】是一种在Windows 8操作系统环境下,为C#开发的应用程序设计的用户界面风格。...同时,随着技术的演进,如UWP(Universal Windows Platform)的出现,这些技能也将有助于适应未来Windows平台的发展。
Windows 8引入了一种称为“沉浸式”(Immersive)的界面,这种界面强调简洁、现代的磁贴风格和触控优化。在C#中实现这一设计,开发者需要使用WPF(Windows Presentation Foundation)或者UWP(Universal Windows ...
Windows 8 Metro 风格(现称为 Modern UI 或 UWP 应用)是微软在 Windows 8 操作系统中引入的一种全新的用户界面设计语言。这种设计语言强调简洁、清晰、易于触摸操作的特点,并且采用大块的纯色色块来构建应用程序...
### Win8.1系统中的Runtime Broker进程:是否可以禁用? #### RuntimeBroker 进程概述 在使用Windows 8.1操作系统的用户中,经常会在任务管理器中发现一个名为“RuntimeBroker”的进程。该进程在Windows 8及之后...
在Windows操作系统中,"应用"通常指的是Windows应用商店中的现代应用程序,这些应用程序是为Windows 10、11等设计的,与传统的桌面程序(.exe文件)有所不同。本篇文章将深入探讨Windows应用的各个方面,包括其特点...
10. **Continuum**:针对不同设备类型,Windows 10引入了Continuum功能,使得Modern Apps可以根据设备的形态(例如,桌面模式或平板模式)自动调整UI布局。 11. **App Service and Background Tasks**:开发者可以...
- **图标大小**:规定了不同平台(WPF、UWP、WinUI、Win32)所需的图标尺寸。 - **图标缩放**:确保图标在不同分辨率下的清晰度。 **4.3.1 图标大小(WPF、UWP、WinUI)** - **尺寸要求**:列出这些平台下图标的...
随着Windows 8的发布,微软推出了一种全新的应用类型——Metro风格的应用程序(现在被称为Modern或UWP应用)。这类应用专为触摸屏设备设计,同时也适用于传统的桌面电脑。它们的设计理念强调简洁、直观和高效,旨在...