I'm trying to write a small library in C# to reuse some classes and UI Element into differents projects.
A UI Element I want to put in this library is a very simple UserControl :
<UserControl
x:Class="MyProjet.UiUtil.Progress"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyProjet.UiUtil"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<UserControl.Resources>
<Style x:Key="BasicTextStyle" TargetType="TextBlock">
<Setter Property="Foreground" Value="{StaticResource ApplicationForegroundThemeBrush}"/>
<Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/>
<Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/>
<Setter Property="TextTrimming" Value="WordEllipsis"/>
<Setter Property="TextWrapping" Value="Wrap"/>
<Setter Property="Typography.StylisticSet20" Value="True"/>
<Setter Property="Typography.DiscretionaryLigatures" Value="True"/>
<Setter Property="Typography.CaseSensitiveForms" Value="True"/>
</Style>
<Style x:Key="BaselineTextStyle" TargetType="TextBlock" BasedOn="{StaticResource BasicTextStyle}">
<Setter Property="LineHeight" Value="20"/>
<Setter Property="LineStackingStrategy" Value="BlockLineHeight"/>
<!-- Aligne correctement le texte sur sa ligne de base -->
<Setter Property="RenderTransform">
<Setter.Value>
<TranslateTransform X="-1" Y="4"/>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="HeaderTextStyle" TargetType="TextBlock" BasedOn="{StaticResource BaselineTextStyle}">
<Setter Property="FontSize" Value="56"/>
<Setter Property="FontWeight" Value="Light"/>
<Setter Property="LineHeight" Value="40"/>
<Setter Property="RenderTransform">
<Setter.Value>
<TranslateTransform X="-2" Y="8"/>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="SubheaderTextStyle" TargetType="TextBlock" BasedOn="{StaticResource BaselineTextStyle}">
<Setter Property="FontSize" Value="26.667"/>
<Setter Property="FontWeight" Value="Light"/>
<Setter Property="LineHeight" Value="30"/>
<Setter Property="RenderTransform">
<Setter.Value>
<TranslateTransform X="-1" Y="6"/>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid x:Name="GridRoot" Width="{Binding Path=GridWidth}" Height="{Binding Path=GridHeight}" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="140" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock x:Name="TxtTitle" Grid.Row="0" Text="{Binding Path=Title}" Style="{StaticResource HeaderTextStyle}" TextAlignment="Center" TextWrapping="Wrap" Margin="12,12,12,0" VerticalAlignment="Center" HorizontalAlignment="Center" />
<StackPanel Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock x:Name="TxtDescription" Text="{Binding Path=Description}" TextAlignment="Center" Style="{StaticResource SubheaderTextStyle}" TextWrapping="Wrap" />
<ProgressBar IsIndeterminate="True" Margin="0, 15" Width="350" />
</StackPanel>
</Grid>
</UserControl>
and the code behind :
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace MyProjet.UiUtil
{
public sealed partial class Progress
{
/// <summary>
/// Inner class for MVVM.
/// </summary>
public sealed class ViewModelProgress
{
public string Title { get; set; }
public string Description { get; set; }
public double GridHeight
{
get
{
return Window.Current.Bounds.Height;
}
}
public double GridWidth
{
get
{
return Window.Current.Bounds.Width;
}
}
}
/// <summary>
/// Default constructor.
/// </summary>
/// <param name="title"></param>
/// <param name="description"></param>
/// <param name="backgroundColor"></param>
/// <param name="titleForeground"></param>
/// <param name="descriptionForeground"></param>
public Progress(string title, string description, Brush backgroundColor, Brush titleForeground, Brush descriptionForeground)
{
InitializeComponent();
GridRoot.Background = backgroundColor;
TxtTitle.Foreground = titleForeground;
TxtDescription.Foreground = descriptionForeground;
this.DataContext = new ViewModelMBProgressHUD() { Title = title, Description = description };
}
}
}
Here an example how to use this user control into the MainPage of a Windows Store App :
using System;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace SampleProject
{
public sealed partial class MainPage
{
private Popup _popup;
private DispatcherTimer _timer;
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
_popup = new Popup() { Child = new Progress("Mon Titre", "Ma description", new SolidColorBrush(Colors.Magenta), new SolidColorBrush(Colors.Green), new SolidColorBrush(Colors.Orange)), IsOpen = false };
_timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(5) };
_timer.Tick += timer_Tick;
}
private void timer_Tick(object sender, object e)
{
_timer.Stop();
_popup.IsOpen = false;
BtnDisplay.IsEnabled = true;
}
private void BtnDisplay_Click(object sender, RoutedEventArgs e)
{
BtnDisplay.IsEnabled = false;
_timer.Start();
_popup.IsOpen = true;
}
}
}
When I copy the UserControl files into the Sample project, everything works well, but I have an issue when I use the UserControl from the library (package as a nuget dependency), I have the following error message and code :
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
Windows.UI.Xaml.Controls.Frame.NavigationFailed was unhandled.
I have read on the Internet that comment or uncomment this line can help :
base.OnNavigatedTo(e);
In my case, it doesn't change anything...
Hope someone will be able to help me !
Thk you in advance !