c# - 如何为 UWP 自定义控件创建 nuget 包
问题描述
我有这个用户控件,我想打包到一个 nuget 包中。我的 xaml 和 xaml.cs 文件位于一个空白的 uwp 应用程序中,我成功地将其打包到一个 nuget 包中。问题是,如果我在另一个项目中安装 nuget,我无法真正引用它。知道我应该如何构建这个 buget 吗?我从 MS 的教程中读到我可能应该使用模板化控件或类似的东西,但在这种情况下我如何编码我的 xaml。真的需要一些建议。
xml:
<UserControl
x:Class="StickControl.StickControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:StickControl"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid>
<Grid x:Name="Stick"></Grid>
<ListView Width="auto" Height="auto" ItemsSource="{x:Bind Items,Mode=OneWay}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Background="White" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<TextBlock FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5" Foreground="Black" Text="{Binding menuText,Mode=OneWay}"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
C#:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace StickControl
{
public sealed partial class Stick : Page
{
public ObservableCollection<MenuItem> Items
{
get { return (ObservableCollection<MenuItem>)GetValue(TitleListProperty); }
set { SetValue(TitleListProperty, value); RaisePropertyChanged(); }
}
public static readonly DependencyProperty TitleListProperty =
DependencyProperty.Register("Items", typeof(ObservableCollection<MenuItem>), typeof(StickControl), null);
public List<string> Titles
{
get { return (List<string>)GetValue(TitlesProperty); }
set
{
SetValue(TitlesProperty, value); RaisePropertyChanged();
if (Items.Count < Titles.Count)
{
int dif = Titles.Count - Items.Count;
for (int i = 0; i < dif; i++)
Items.Add(new MenuItem());
}
for (int i = 0; i < Titles.Count; i++)
{
Items[i].menuText = Titles[i];
}
}
}
public static readonly DependencyProperty TitlesProperty =
DependencyProperty.Register("Titles", typeof(List<string>), typeof(StickControl), null);
public Stick()
{
this.InitializeComponent();
Items = new ObservableCollection<MenuItem>();
}
public void Remove(int count)
{
for (int i = Items.Count - 1; i > Items.Count - count - 1; i--)
Items.RemoveAt(i);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public class MenuItem : INotifyPropertyChanged
{
private string menu_text;
public string menuText
{
get { return menu_text; }
set { menu_text = value; RaisePropertyChanged(); }
}
public MenuItem()
{
menuText = "(empty title)";
}
public MenuItem(string title)
{
menuText = title;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string name = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
}
解决方案
@logeshpalani98 提供的链接仅适用于 SDK 样式项目(由但不限于 .NET Core 和 .NET Standard 使用)。UWP 项目尚不支持 SDK 样式项目。但是 NuGet 的文档确实有另一个标题为Create UWP packages的页面。不幸的是,这比打包一个 SDK 风格的项目要困难得多,但它应该能让你走上正确的道路。
另一种选择是找到带有 UWP 控件的现有包,例如Telerik.UI.for.UniversalWindowsPlatform并弄清楚它是如何工作的(哪些文件在哪些目录中),然后创建自己的包来复制结构。
推荐阅读
- python - 异常是标量类型 Float 的预期对象,但参数 #2 'other' 得到标量类型 Long
- javascript - Electron 忽略 @css prefers-color-scheme
- javascript - 如何使用 firebase 增量功能?
- javascript - 发送多个附件时,Nodemailer 崩溃
- python - 反转字典并将其原始键作为列表中的值
- python - Python 可以通过 ctypes 并行调用 C 函数以提高速度吗?
- php - 如何使用 PHP 正确管理出价系统?
- algorithm - 对于给定的两个整数 A 和 B,找到一对数字 X 和 Y 使得 A = X*Y 和 B = X xor Y
- java - 如何在网站 URL 后面加上参考号?
- safari - 导航到新页面时显示 iOS 13 PWA URL 栏