将UserControl从Windows Phone 8.1转换为通用应用程序10

时间:2016-04-21 13:02:15

标签: c# wpf xaml windows-phone-8 win-universal-app

我需要你的建议和帮助。我已经有一个工作的Windows Phone 8.1应用程序,我需要将其转换为UWP。

我的问题与usercontrol自定义图像按钮有关:如何转换它以及在UWP上进行此类控制的最佳方法是什么。

这是我的Windows Phone 8代码: -

XAML: -

<UserControl x:Class="CustomizedPopup.ImageButton.ImageButtonControl">
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <!--<Image x:Name="Image" Stretch="Fill"  Width="auto" Grid.Row="0"/>-->
    <TextBlock x:Name="TextBlock" Grid.Row="0"  VerticalAlignment="Center"  HorizontalAlignment="Center" TextAlignment="Center" />
</Grid>
</UserControl>

CS档案: -

namespace CustomizedPopup.ImageButton  
{
public partial class ImageButtonControl : UserControl
{
    public ImageButtonControl()
    {
        InitializeComponent();
    }

    ImageSource source;
    public event EventHandler Click;


    Double textfontsize;
    public Double TextFontSize
    {
        get { return textfontsize; }
        set { textfontsize = value; TextBlock.FontSize = textfontsize; }
    }
    public ImageSource Source
    {

        get { return source; }
        set
        {
            source = value;
            System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
            Image image = new Image();
            image.Source = source;// new System.Windows.Media.Imaging.BitmapImage(new Uri(@"/Assets/bg/bg1.jpg/", UriKind.RelativeOrAbsolute));
            myBrush.ImageSource = image.Source;
            myBrush.Stretch = Stretch.Uniform;
            LayoutRoot.Background = myBrush;

            //Image.Source = source; 
        }
    }

    public ImageSource PressedSource { get; set; }

    string text;
    public string Text
    {
        get { return text; }
        set { text = value; TextBlock.Text = text; }
    }

    protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);
        System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
        Image image = new Image();
        image.Source = PressedSource;// new System.Windows.Media.Imaging.BitmapImage(new Uri(@"/Assets/bg/bg1.jpg/", UriKind.RelativeOrAbsolute));
        myBrush.ImageSource = image.Source;
        myBrush.Stretch = Stretch.Uniform;
        LayoutRoot.Background = myBrush;

    }

    protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
    {
        base.OnMouseLeave(e);

        System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
        Image image = new Image();
        image.Source = Source;// new System.Windows.Media.Imaging.BitmapImage(new Uri(@"/Assets/bg/bg1.jpg/", UriKind.RelativeOrAbsolute));
        myBrush.ImageSource = image.Source;
        myBrush.Stretch = Stretch.Uniform;
        LayoutRoot.Background = myBrush;
    }

    protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonUp(e);
        //Image.Source = Source;

        System.Windows.Media.ImageBrush myBrush = new System.Windows.Media.ImageBrush();
        Image image = new Image();
        image.Source = Source;// new System.Windows.Media.Imaging.BitmapImage(new Uri(@"/Assets/bg/bg1.jpg/", UriKind.RelativeOrAbsolute));
        myBrush.ImageSource = image.Source;
        myBrush.Stretch = Stretch.Uniform;
        LayoutRoot.Background = myBrush;

        if (Click != null)
            Click(this, EventArgs.Empty);

        //Image.Stretch = Stretch.Uniform;
    }
}
}

1 个答案:

答案 0 :(得分:0)

要将Windows 8.1应用程序转换为UWP,您可以使用此脚本

Powershell script to upgrade V8.1 csproj and manifest to uwp

但它不会转换您的代码 如果谈论代码,您可以保留XAML,但是您需要更改C#。 替换所有电话,如

OnMouseLeave(System.Windows.Input.MouseEventArgs e)

OnPointerExited(PointerRoutedEventArgs e)

ImageBrush现在位于Windows.UI.Xaml.Media命名空间中。改变它