2016-04-21 67 views
0

我需要您的建议和帮助。我已经有一个工作的Windows Phone 8.1应用程序,我需要将其转换为UWP。将UserControl从Windows Phone 8.1转换为通用应用程序中的问题10

我的问题是关于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; 
    } 
} 
} 

回答

0

对于转换Windows 8.1应用UWP你可以使用这个脚本

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

但它不会转换你的代码。
如果谈论代码,您可以保持原样,但需要对C#进行更改。 更换所有通话像

OnMouseLeave(System.Windows.Input.MouseEventArgs e) 

随着

OnPointerExited(PointerRoutedEventArgs e) 

而且图像刷现在是Windows.UI.Xaml.Media命名空间。更改它也

相关问题