簡體   English   中英

如何為UWP自定義控件創建nuget包

[英]how to create a nuget package for UWP custom control

我有要打包到nuget包中的用戶控件。 我的xaml和xaml.cs文件位於一個空白的uwp應用程序中,我將其成功打包到nuget程序包中。 問題是,如果我在另一個項目中安裝nuget,則無法真正引用它。 知道我應該如何構建這個Buget嗎? 我從MS的教程中讀到,我可能應該使用模板化控件或類似的控件,但是在這種情況下,如何編碼我的xaml。 確實需要一些建議。

XAML:

<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的文檔中確實有另一頁名為“ 創建UWP軟件包” 不幸的是,這比打包SDK風格的項目要困難得多,但是它應該使您正確地開始。

另一個選擇是找到一個具有UWP控件的現有程序包,例如Telerik.UI.for.UniversalWindowsPlatform並弄清楚它的工作方式(哪些文件位於哪些目錄中),然后創建自己的程序包來復制結構。

進入Packages並輸入您的Nuget詳細信息,然后右鍵單擊該項目並選擇Pack,然后選中Generate Nuget Package 轉到Project文件夾。那里有.Nuspec文件。

更多細節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM