Bind a Path Stroke color to Foreground - silverlight-4.0

Using the TabControl element for Silverlight in Blend I created the following markup:
<controls:TabControl>
<controls:TabItem Header="TabItem" Style="{StaticResource TabItemStyle1}" />
<controls:TabItem Style="{StaticResource TabItemStyle1}">
<controls:TabItem.Header>
<StackPanel Orientation="Horizontal">
<Path Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14"
StrokeLineJoin="Round" Margin="0 0 6 0"
Stroke="Black"/>
<TextBlock Text="TabItem"/>
</StackPanel>
</controls:TabItem.Header>
</controls:TabItem>
</controls:TabControl>
TabItemStyle1 is a copy of the default style of a TabItem.
I altered TabItemStyle1 by adding a color animation in the MouseOver storyboard so that unselected tab items become red when the mouse hovers them:
<ColorAnimation BeginTime="0" Duration="00:00:00.001"
Storyboard.TargetName="HeaderTopUnselected"
Storyboard.TargetProperty="(UIElement.Foreground).(SolidColorBrush.Color)"
To="Red" />
Now when I hover the second tab, the text becomes red but the Path remains black:
How should I define the Path Stroke color to make it follow the same rule?

The following should work:
<controls:TabControl>
<controls:TabItem Header="TabItem" Style="{StaticResource TabItemStyle1}" />
<controls:TabItem Style="{StaticResource TabItemStyle1}">
<controls:TabItem.Header>
<StackPanel Orientation="Horizontal">
<Path Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14"
StrokeLineJoin="Round" Margin="0 0 6 0"
Stroke="{Binding ElementName=textBlock, Path=Foreground}"/>
<TextBlock x:Name="textBlock" Text="TabItem"/>
</StackPanel>
</controls:TabItem.Header>
</controls:TabItem>
</controls:TabControl>

it's not a perfect solution but you could use this
<sdk:TabControl>
<sdk:TabItem Header="item1"></sdk:TabItem>
<sdk:TabItem Foreground="Red" x:Name="someNameForTheTab">
<sdk:TabItem.Header>
<StackPanel Orientation="Horizontal">
<!--Just set stroke binding to the foreground of the tabItem-->
<Path Stroke="{Binding Foreground, ElementName=someNameForTheTab}" Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14"
StrokeLineJoin="Round" Margin="0 0 6 0"/>
<TextBlock Text="item2"/>
</StackPanel>
</sdk:TabItem.Header>
</sdk:TabItem>
</sdk:TabControl>

Try binding to the TemplatedParent like this:
<Path
Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14"
StrokeLineJoin="Round"
Margin="0 0 6 0"
Stroke="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"/>
I haven't tested this, but give it a whirl and let me know. If it doesn't work, try this:
<Path Data="M0,14L0,6 5,0 10,6 10,14 0,6 10,6 0,14 10,14" StrokeLineJoin="Round" Margin="0 0 6 0">
<Path.Stroke>
<SolidColorBrush Color="{Binding Foreground.Color, RelativeSource={RelativeSource TemplatedParent}}" />
</Path.Stroke>
</Path>
I have a feeling that the Color property needs to be the source of binding, not the actual brush.

I made it work by binding the header content brushes to {TemplateBinding TextElement.Foreground}.
In other cases I used standard property binding with converters, for example if I had to adapt element's brushes to item state.

// animazione periferica
public static void LineAnimation(Line _line,String _colore)
{
Storyboard result = new Storyboard();
Duration duration = new Duration(TimeSpan.FromSeconds(2));
ColorAnimation animation = new ColorAnimation();
animation.RepeatBehavior = RepeatBehavior.Forever;
animation.Duration = duration;
switch (_colore.ToUpper())
{
case "RED":
animation.From = Colors.Red;
break;
case "ORANGE":
animation.From = Colors.Orange;
break;
case "YELLOW":
animation.From = Colors.Yellow;
break;
case "GRAY":
animation.From = Colors.DarkGray;
break;
default:
animation.From = Colors.Green;
break;
}
animation.To = Colors.Gray;
Storyboard.SetTarget(animation, _line);
Storyboard.SetTargetProperty(animation, new PropertyPath("(Line.Stroke).(SolidColorBrush.Color)"));
result.Children.Add(animation);
result.Begin();
}
}
//**********************************************
public partial class MainPage : UserControl
{
public Line _line;
public MainPage()
{
InitializeComponent();
Canvas.MouseLeftButtonDown += Canvas_MouseLeftButtonDown;
Canvas.MouseLeftButtonUp += Canvas_MouseLeftButtonUp;
}
void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_line.X2 = e.GetPosition(this.Canvas).X;
_line.Y2 = e.GetPosition(this.Canvas).Y;
_line.Loaded += _line_Loaded;
Canvas.Children.Add(_line);
}
void _line_Loaded(object sender, RoutedEventArgs e)
{
Cls_Barriere.LineAnimation(sender as Line, "RED");
}
void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_line = new Line();
_line.Stroke = new SolidColorBrush(Colors.White);
_line.StrokeThickness = 5;
_line.StrokeStartLineCap = PenLineCap.Round;
_line.StrokeEndLineCap = PenLineCap.Round;
_line.StrokeDashCap = PenLineCap.Round;
_line.X1 = e.GetPosition(this.Canvas).X;
_line.Y1= e.GetPosition(this.Canvas).Y;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
}

Related

How to change NavigationView Header when navigating to page?

The goal I'm trying to accomplish here is to change the NavigationView Header property when I press a NavigationView MenuItem, where it switches to a different page. I want the header to display the text on the button that was pressed but I'm very much learning to use the WinUI/XAML library still and looking at the gallery and documentation isn't really helping.
NavigationView.MenuItems on MainPage.xaml
<muxc:NavigationView.MenuItems>
<muxc:NavigationViewItem Icon="Home" Content="Home" x:Name="Menu1Item" Tag="Page1"/>
<muxc:NavigationViewItem Icon="Contact" Content="Account" x:Name="Menu2Item" Tag="Page2"/>
<muxc:NavigationViewItem Icon="Bullets" Content="Attendance" x:Name="Menu3Item" Tag="Page3"/>
<muxc:NavigationViewItem Icon="Library" Content="Grades" x:Name="Menu4Item" Tag="Page4"/>
<muxc:NavigationViewItem Icon="Flag" Content="Grad Reqs" x:Name="Menu5Item" Tag="Page5"/>
</muxc:NavigationView.MenuItems>
https://i.stack.imgur.com/ZeNdf.png
https://i.stack.imgur.com/t7eEj.png
EDIT:
MainPage.xaml:
<Page
xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
x:Class="ProjectHurricanes.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ProjectHurricanes"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
muxc:BackdropMaterial.ApplyToRootOrPageBackground="True"
>
<Page.Resources>
<!--This top margin is the height of the custom TitleBar-->
<Thickness x:Key="NavigationViewContentMargin">0,48,0,0</Thickness>
</Page.Resources>
<Grid>
<Border x:Name="AppTitleBar"
IsHitTestVisible="True"
VerticalAlignment="Top"
Background="Transparent"
Height="40"
Canvas.ZIndex="1"
Margin="48,8,0,0">
<StackPanel Orientation="Horizontal">
<Image x:Name="AppFontIcon"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Source="Assets/Square44x44Logo.png"
Width="16"
Height="16"/>
<TextBlock x:Name="AppTitle"
Text="Project"
VerticalAlignment="Center"
Margin="12, 0, 0, 0"
Style="{StaticResource CaptionTextBlockStyle}" />
</StackPanel>
</Border>
<muxc:NavigationView x:Name="NavigationViewControl"
IsTitleBarAutoPaddingEnabled="False"
IsBackButtonVisible="Visible"
Header="Home"
DisplayModeChanged="NavigationViewControl_DisplayModeChanged"
SelectionFollowsFocus="Enabled"
ItemInvoked="NavigationView_ItemInvoked"
PaneDisplayMode="Left"
Canvas.ZIndex="0">
<muxc:NavigationView.MenuItems>
<muxc:NavigationViewItem Icon="Home" Content="Home" x:Name="Menu1Item" Tag="Page1"/>
<muxc:NavigationViewItem Icon="Contact" Content="Account" x:Name="Menu2Item" Tag="Page2"/>
<muxc:NavigationViewItem Icon="Bullets" Content="Attendance" x:Name="Menu3Item" Tag="Page3"/>
<muxc:NavigationViewItem Icon="Library" Content="Grades" x:Name="Menu4Item" Tag="Page4"/>
<muxc:NavigationViewItem Icon="Flag" Content="Grad Reqs" x:Name="Menu5Item" Tag="Page5"/>
</muxc:NavigationView.MenuItems>
<Grid Padding="20">
<Frame x:Name="rootFrame"/>
</Grid>
</muxc:NavigationView>
</Grid>
</Page>
MainPage.xaml.cs:
OnItemInvoked:
private void NavigationView_OnItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sender, Microsoft.UI.Xaml.Controls.NavigationViewItemInvokedEventArgs args)
{
FrameNavigationOptions navOptions = new FrameNavigationOptions();
navOptions.TransitionInfoOverride = args.RecommendedNavigationTransitionInfo;
#pragma warning disable IDE0007 // Use implicit type
string navItemTag = args.InvokedItemContainer.Tag.ToString();
#pragma warning restore IDE0007 // Use implicit type
Type pageType = null;
if (navItemTag == "Page1")
{
pageType = typeof(Page1);
}
else if (navItemTag == "Page2")
{
pageType = typeof(Page2);
}
else if (navItemTag == "Page3")
{
pageType = typeof(Page3);
}
else if (navItemTag == "Page4")
{
pageType = typeof(Page4);
}
else if (navItemTag == "Page5")
{
pageType = typeof(Page5);
}
if (pageType == null)
{
return;
}
rootFrame.NavigateToType(pageType, null, navOptions);
}
You can add the ItemInvoked event to your NavigationView and in the event you can simple get the Content of the selected item.
An using-directive to muxc
using muxc = Microsoft.UI.Xaml.Controls;
The event in your MainPage.xaml.cs
private void NavigationView_ItemInvoked(Microsoft.UI.Xaml.Controls.NavigationView sender, Microsoft.UI.Xaml.Controls.NavigationViewItemInvokedEventArgs args)
{
if (NavigationView.SelectedItem is muxc.NavigationViewItem item)
{
sender.Header = item.Content.ToString();
}
}
The event added to your NavigationView
<muxc:NavigationView ItemInvoked="NavigationView_ItemInvoked" x:Name="NavigationView">

How to create an identical button to the one in 'Settings' page?

I want to create buttons on my UWP/XAML app similar to the buttons on the Windows 'Settings' page using the WinUI 3 library. The gallery didn't show this kind of button with an icon and a title/description. I'm wondering how it can be accomplished since I am developing an app that mostly serves as a portal to go to websites and navigate easily through web/PC apps. Any help would be appreciated.
I want the buttons to simply redirect the user to a NavigationView page (Page5.xaml)
I have a NavigationView on MainPage.xaml and these buttons are going on Page4, so I'm not sure how I can program the buttons to go to Page5.xaml
https://i.stack.imgur.com/7Uff8.png
You can create a usercontrol and use some of my code:
MyUserControl.xaml:
<Grid x:Name="MainGrid" PointerEntered="Grid_PointerEntered" PointerExited="Grid_PointerExited" PointerPressed="FontIcon_PointerPressed" Margin="20,5,20,5" Height="70" CornerRadius="5" Padding="{StaticResource ExpanderHeaderPadding}" HorizontalAlignment="Stretch" Background="{ThemeResource ExpanderHeaderBackground}" BorderThickness="{ThemeResource ExpanderHeaderBorderThickness}" BorderBrush="{ThemeResource ExpanderHeaderBorderBrush}">
<StackPanel Orientation="Horizontal" Margin="0,20,0,20">
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="" Margin="0,0,20,0"/>
<TextBlock FontSize="24" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Font"/>
</StackPanel>
<FontIcon FontFamily="Segoe MDL2 Assets" Glyph="" HorizontalAlignment="Right" Margin="0,0,20,0"/>
</Grid>
MyUserControl1.xaml.cs:
public MyUserControl1()
{
this.InitializeComponent();
var color = (MainGrid.Background as SolidColorBrush).Color;
color.A = 20;
MainGrid.Background = new SolidColorBrush(color);
}
private void FontIcon_PointerPressed(object sender, PointerRoutedEventArgs e)
{
if (e.GetCurrentPoint(sender as UIElement).Properties.IsLeftButtonPressed)
{
//Do whatever you want
Debug.WriteLine("Pressed");
}
}
//Change the color on hover:
private void Grid_PointerEntered(object sender, PointerRoutedEventArgs e)
{
var color = (MainGrid.Background as SolidColorBrush).Color;
color.A = 50;
MainGrid.Background = new SolidColorBrush(color);
}
private void Grid_PointerExited(object sender, PointerRoutedEventArgs e)
{
var color = (MainGrid.Background as SolidColorBrush).Color;
color.A = 20;
MainGrid.Background = new SolidColorBrush(color);
}

UWP: how to reflow controls based on grid width

I have two buttons placed horizontally. They are inside a grid. This grid containing two buttons has width 370. When the text on the button becomes large, it needs width more than 370. So what I want to do is, instead of placing then horizontally, I want to place them vertically dynamically when text will start cropping. Basically, I want auto-reflow behavior inside this grid for these two buttons based on width of the grid (not based on width of main window). So I want them to fit in 370 width and if they cannot, I want them to place themselves vertically. How can I achieve this?
I explored GridView but it will show buttons inside the box that comes with GridView so I do not want extra UI that comes with GridView unless we have an option to hide it?
I checked AdaptiveTrigger but that is based on width of window not of the control (grid in this case)
<Grid
Grid.Row="2"
Margin="0,36,0,28"
Width="370"
HorizontalAlignment="Left"
VerticalAlignment="Bottom">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid
Grid.Column="0"
CornerRadius="3">
<Button
MinWidth="118"
MinHeight="30"
HorizontalAlignment="Left"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
AutomationProperties.Name="{x:Bind ViewModel.PrimaryActionAutomationName, Mode=OneWay}"
BorderThickness="1"
Click="{x:Bind ViewModel.InvokePrimaryAction}"
Content="{x:Bind ViewModel.PrimaryAction, Mode=OneWay}"
CornerRadius="3"
Style="{StaticResource AccentButtonStyle}" />
</Grid>
<Grid
Grid.Column="1"
CornerRadius="3"
Margin="32,0,0,0">
<HyperlinkButton
AutomationProperties.Name="{x:Bind ViewModel.SecondaryLinkAutomationName, Mode=OneWay}"
AutomationProperties.AutomationId="{Binding AutomationId, ConverterParameter=HyperlinkButton, Converter={StaticResource AutomationIdConverter}}"
Content="{x:Bind ViewModel.SecondaryText, Mode=OneWay}"
FontSize="14"
Margin="0,0,0,0"
Style="{StaticResource HyperlinkButtonStyle}"
NavigateUri="{x:Bind ViewModel.SecondaryLink, Mode=OneWay}" />
</Grid>
</Grid>
For your scenario, I'd suggest you custom a StateTrigger based on the StateTriggerBase Class to monitor the width of the Grid and apply visual states based on the width.
I've made a simple sample that you could refer to.
ControlSizeTrigger:
public class ControlSizeTrigger : StateTriggerBase
{
//private variables
private double _minHeight, _minWidth = -1;
private FrameworkElement _targetElement;
private double _currentHeight, _currentWidth;
//public properties to set from XAML
public double MinHeight
{
get
{
return _minHeight;
}
set
{
_minHeight = value;
}
}
public double MinWidth
{
get
{
return _minWidth;
}
set
{
_minWidth = value;
}
}
public FrameworkElement TargetElement
{
get
{
return _targetElement;
}
set
{
if (_targetElement != null)
{
_targetElement.SizeChanged -= _targetElement_SizeChanged;
}
_targetElement = value;
_targetElement.SizeChanged += _targetElement_SizeChanged;
}
}
//Handle event to get current values
private void _targetElement_SizeChanged(object sender, SizeChangedEventArgs e)
{
_currentHeight = e.NewSize.Height;
_currentWidth = e.NewSize.Width;
UpdateTrigger();
}
//Logic to evaluate and apply trigger value
private void UpdateTrigger()
{
//if target is set and either minHeight or minWidth is set, proceed
if (_targetElement != null && (_minWidth > 0 || _minHeight > 0))
{
//if both minHeight and minWidth are set, then both conditions must be satisfied
if (_minHeight > 0 && _minWidth > 0)
{
SetActive((_currentHeight >= _minHeight) && (_currentWidth >= _minWidth));
}
//if only one of them is set, then only that condition needs to be satisfied
else if (_minHeight > 0)
{
SetActive(_currentHeight >= _minHeight);
}
else
{
SetActive(_currentWidth >= _minWidth);
bool bbb = _currentWidth >= _minWidth;
Debug.WriteLine("Widthtrigger :" + bbb);
}
}
else
{
SetActive(false);
}
}
}
MainPage.xaml:
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ControlSizeStates">
<VisualState x:Name="SizeChange">
<VisualState.StateTriggers>
<local:ControlSizeTrigger MinWidth="800" TargetElement="{x:Bind Path=MyStackPanel}" />
<!--<AdaptiveTrigger MinWindowHeight="500" />-->
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="MyStackPanel.Background" Value="Red" />
<Setter Target="MyStackPanel.Orientation" Value="Horizontal" />
<Setter Target="MyButton.Foreground" Value="Black" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackPanel x:Name="MyStackPanel" Width="100" Height="200" Background="AliceBlue" Orientation="Vertical">
<Button x:Name="MyButton" Foreground="Red" Content="Click" Click="Button_Click"/>
<Button x:Name="MyButton2" Foreground="Red" Content="Click" />
</StackPanel>
</Grid>
MainPage.cs:
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MyStackPanel.Width = 1100;
}

Crop image with rectangle

I have a Image and I want crop it by using a rectangle, code below is the code I put a image and draw a rectangle at middle of the image:
MainPage.Xaml:
<Canvas x:Name="canvas" HorizontalAlignment="Center" VerticalAlignment="Center" Width="340" Height="480" Background="Blue">
<Image x:Name="photo" HorizontalAlignment="Center" VerticalAlignment="Center" ManipulationMode="All">
<Image.RenderTransform>
<CompositeTransform/>
</Image.RenderTransform>
</Image>
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<RectangleGeometry Rect="0,0,340,480"/>
</Path.Data>
</Path>
</Canvas>
I successful display the image and draw a rectangle. The sample image as below:
Now I want click a button to crop the image within the rectangle(not auto clip). The rectangle is auto added when image loaded. So cannot use "Point Pressed" and "Point Released". And also cannot use "rectangle.clip" because it will auto clip the image. How do I solve it? Thanks
Updated:
I able to move the image,How do I bind the data and set the rectangle coordinate to dynamic? Code below is to transform the image. Thanks.
public sealed partial class MainPage: Page
{
private CompositeTransform compositeTranslation;
public MainPage()
{
this.InitializeComponent();
photo.ManipulationDelta += Composite_ManipulationDelta;
compositeTranslation = new CompositeTransform();
photo.RenderTransform = this.compositeTranslation;
}
void Composite_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
// scale the image.
compositeTranslation.CenterX = photo.ActualWidth / 2;
compositeTranslation.CenterY = photo.ActualHeight / 2;
compositeTranslation.ScaleX *= e.Delta.Scale;
compositeTranslation.ScaleY *= e.Delta.Scale;
compositeTranslation.TranslateX += e.Delta.Translation.X;
compositeTranslation.TranslateY += e.Delta.Translation.Y;
}
}
I have not used or XAML as it creates confusion for me. So I created a snippet according to you problem. Try that and let me know the results. I have used the same image as you posted
XAML
<Page.BottomAppBar>
<AppBar IsSticky="True" IsOpen="True">
<Button Content="Crop" Click="btnCrop_Click" />
</AppBar>
</Page.BottomAppBar>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Image x:Name="photo" HorizontalAlignment="Center" VerticalAlignment="Center" ManipulationMode="All" Source="http://i.stack.imgur.com/UIBSp.png" />
<Path x:Name="path" Stroke="Red" StrokeThickness="3">
<Path.Data>
<RectangleGeometry Rect="545,212,440,420"/>
</Path.Data>
</Path>
</Grid>
C#
private void btnCrop_Click(object sender, RoutedEventArgs e)
{
var _rect = new RectangleGeometry();
_rect.Rect = path.Data.Bounds;
photo.Clip = _rect;
}

Changing the orientation of a user control (ProgressBar) in WP7?

I'm using a progress bar in my app, this progress bar is defined inside the user control, e.g.:
UserControl x:Class="StirLibrary.ProgressBarControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="480">
<Grid x:Name="LayoutRoot" Height="800">
<Border BorderThickness="2" BorderBrush="Transparent" Background="Transparent" Margin="50,522,50,158">
<StackPanel>
<TextBlock Text="Loading..." Name="loading" Grid.Row="1" HorizontalAlignment="Center" Height="30" Foreground="Green">
</TextBlock>
<ProgressBar Background="Transparent" Margin="10, 0, 0, 10" Height="80" HorizontalAlignment="Center" Name="progressBar1" VerticalAlignment="Top" Width="380" Grid.Row="2" HorizontalContentAlignment="Left" IsHitTestVisible="True" VerticalContentAlignment="Top" Value="0" Maximum="100">
</ProgressBar>
</StackPanel>
</Border>
</Grid>
</UserControl>
My problem is when the orientation of my app changes to landscape the progress bar's orientation doesn't change and this makes the app look ugly. Any suggestions how to avoid this and make the progress bar displayed as per orientation are welcome.
As Matt has mentioned above it is not possible to orient a pop up in user control because User control doesn't have any room for supported orientation. but since it was very crucial requirement for our App i found a work around and made few changes in the Main Page's class file and the user control's class file.. the changes are:
private void PhoneApplicationPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
if ((e.Orientation & PageOrientation.Portrait) == PageOrientation.Portrait)
{
ProgressBarControl.getInstance().ProgressBarControl_LayoutUpdated(this, e,e.Orientation.ToString());
}
else if ((e.Orientation & PageOrientation.Landscape) == PageOrientation.Landscape)
{
ProgressBarControl.getInstance().ProgressBarControl_LayoutUpdated(this, e, e.Orientation.ToString());
}
}
These are the changes in MainPage.xaml.cs
public partial class ProgressBarControl : UserControl
{
private static ProgressBarControl instance = null;
public static Popup popup;
private ProgressBarControl()
{
InitializeComponent();
}
public static ProgressBarControl getInstance()
{
if (instance == null)
{
instance = new ProgressBarControl();
popup = new Popup();
popup.Child = instance;
popup.IsOpen = false;
}
return instance;
}
public void ProgressBarControl_LayoutUpdated(object sender, EventArgs e,string orientation)
{
if (orientation == "LandscapeRight")
{
ProgressPanel.RenderTransformOrigin = new Point(0.5, 0.5);
ProgressPanel.RenderTransform = new CompositeTransform { Rotation = 270 };
}
else if(orientation == "LandscapeLeft")
{
ProgressPanel.RenderTransformOrigin = new Point(0.5, 0.5);
ProgressPanel.RenderTransform = new CompositeTransform { Rotation = 90 };
}
else
{
ProgressPanel.RenderTransformOrigin = new Point(0, 0);
ProgressPanel.RenderTransform = new CompositeTransform { Rotation = 0 };
}
}
public static void displayProgressBar(int requestId, int status, string msg)
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if (instance == null)
{
instance = new ProgressBarControl();
popup = new Popup();
popup.Child = instance;
}
popup.IsOpen = true;
instance.loading.Text = msg;
instance.progressBar1.IsIndeterminate = true;
instance.progressBar1.Value = status;
});
}
public static void dismissProgressBar()
{
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
if(popup!=null)
{
popup.IsOpen = false;
}
});
}
}
and this what i have done in my ProgressBarControl.cs file (this is the user control's class file)
Xaml file:
<UserControl x:Class="StirLibrary.ProgressBarControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="480">
<Grid x:Name="LayoutRoot" Height="800">
<!--<Border BorderThickness="2" BorderBrush="Black" Background="Transparent" Margin="54,406,50,320"></Border>-->
<StackPanel x:Name="ProgressPanel" Background="Black" Margin="54,406,50,320">
<TextBlock Text="Loading..." Name="loading" Grid.Row="1" HorizontalAlignment="Center" Height="32" Foreground="White"></TextBlock>
<ProgressBar Background="Green" Margin="10, 0, 0, 10" Height="33" Foreground="White" HorizontalAlignment="Center" Name="progressBar1" VerticalAlignment="Top" Width="351" Grid.Row="2" HorizontalContentAlignment="Left" IsHitTestVisible="True" VerticalContentAlignment="Top" Value="0" Maximum="100"></ProgressBar>
</StackPanel>
</Grid>
</UserControl>
I could enable the orientation for my popup UserControl by simply adding to the Children of the main screen on top of which the Popup is being displayed as:
popUp = new Popup();
loginControl = new LoginPopup(); // this is the custom UserControl
popUp.Child = loginControl;
LayoutRoot.Children.Add(popUp);
The Popup class does not support orientation so you can't use this and expect it to handle orientation changes. This is regardless of whether the control displayed in the popup is in the same assembly or not.
Instead of using a Popup a simple alternative would be to put the control directly on top of all other content on the page. You could include this inside another control (such as a grid or a panel) if you wish.
Manually adding a RotateTransform to the control will give you the ability to add extra control over adjusting the orientation but I'd recommend not going down this route if you can avoid it.