简体   繁体   中英

Issue in Convert UserControl from windows phone 8.1 to universal application 10

I need your suggestion and help please. I already have a working windows phone 8.1 application and I need to convert it to UWP.

My question is related to usercontrol custom image button: how to convert it and what is the best way to have such control on UWP.

This is my windows phone 8 code:-

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 file :-

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;
    }
}
}

For converting Windows 8.1 app to UWP you can use this script

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

But it will not convert your code.
If talk about code, you can leave XAML as is, but you need changes in C#. Replace all calls like

OnMouseLeave(System.Windows.Input.MouseEventArgs e)

With

OnPointerExited(PointerRoutedEventArgs e)

And ImageBrush is now in Windows.UI.Xaml.Media namespace. Change it also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM