Launch WPF Class Library Project Full Screen - c#

I have a WPF class library (with a user control within it being my launch page (similar to MainWindow).
Code Snippet for User control is as follows:
<UserControl x:Class="ABC" ...... >
<Grid>
<Label>Hello World</Label>
</Grid>
</UserControl>
I created another WPF app (labelled Launcher) which would essentially call the this class library.
That project doesnt have any MainWindow (removed it) and am launching the User Control from App.xaml
<Application
x:Class="Launcher.App"
......
StartupUri="pack://application:,,,/Project;component/View/HelloWorld.xaml">
The app doesnt start in full screen ever and always need to maxime it on launching.
I know the answer should be in lines of removing the StartupUri from App.xaml and putting the App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
//What to put here
}
Since there is no MainWindow hence there is I cant do this:
MainWindow wnd = new MainWindow();
wnd.Title = "Something else";
//Set StartupMode = "Maximized"
wnd.Show();

Do you know the Size of your Screen , if so
WindowStyle="None"
WindowStartupLocation="Manual"
Top="0" Left="0"
ResizeMode="NoResize"
BorderThickness="0"
Height="1080" Width="1920"
If you need to figure it out in code behind
WindowStyle="None"
WindowStartupLocation="Manual"
Top="0" Left="0"
ResizeMode="NoResize"
BorderThickness="0"
Height="{Binding Height}" Width="{Binding Width}"
In order to get the size look here :
The First Answer

Related

How to know when window control styling completed

I have a window.xaml which has components with different kind of styling( border color red, opacity changed and so on). In one moment I want to create a screenshot and save to folder. But before that the window background should be transparent and someCanvas should be hidden.
How do I know when the styling method finished so I can take a screenshot?
public void SomeMethod()
{
ChangeWindowControlStyles();
//TODO: waint till 'ChangeWindowControlStyles' finished
TageScreenshotAndSave();
}
public void ChangeWindowControlStyles()
{
this.Background.Opacity = 0;
this.someCanvas.Visibility = Visibility.Collapsed;
//Some other stuff related to window content styling
}
public void TakeScreenshotAndSave()
{
//No multithreading happening here
//Just taking screenshot and saving to folder
}
EDIT
The window itself is transparent WindowStyle="None", that means it has no borders. In the start the window's Background.Opacity is set to 0.1 and all controls are visible (there are other controls than someCanvas that should always be visible).
Before screenshot is taken someCanvas is hidden and the Background.Opacity is set to 0.
Window.xaml
<Window
WindowStartupLocation="CenterScreen"
ResizeMode="NoResize"
WindowState="Maximized"
WindowStyle="None"
AllowsTransparency="True" >
<Window.Background>
<SolidColorBrush Opacity="0.1" Color="White"/>
</Window.Background>
<Grid Name="mainGrid" Background="Transparent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0">
<!--Main canvas, function holder-->
<Canvas Name="canvasAlwaysVisible" Margin="0" Panel.ZIndex="5">
<!-- Controls that are always visible -->
</Canvas>
<Canvas x:Name="someCanvas" Margin="0" Background="Transparent" Visibility="Visibility">
<!-- Controls with styling -->
</Canvas>
</Grid>
</Window>
EDIT 2
Another thing to mention is that inside TakeScreenshotAndSave there is also System.IO operations like - get all folders in directory, creation new directory and so on. Maybe .NET sees that and it is ran asynchronously.
Looks like I found the solution. I don't know why it works, will need to investigate more. That TakeScreenshotAndSave method that I mentioned in code sample was somehow running on different thread. When wrap that method inside Application.Current.Dispatcher it worked!
public void SomeMethod()
{
ChangeWindowControlStyles();
var m_dispatcher = Application.Current.Dispatcher;
m_dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle,
new System.Threading.ThreadStart(delegate
{
TakeScreenshotAndSave(); //Now running on UI thread
}));
}

WPF Thumb drag and move with WindowState.Maximized

I have custom window with WindowState=WindowState.Maximized with border and thumb inside in the border, it seems that when the WindowState=WindowState.Maximized I cannot drag and move the custom window to different screen.
Xaml:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="350"
Width="525"
WindowStyle="None">
<Border Name="headerBorder"
Width="Auto"
Height="50"
VerticalAlignment="Top"
CornerRadius="5,5,0,0"
DockPanel.Dock="Top"
Background="Red"
BorderThickness="1,1,1,1"
BorderBrush="Yellow">
<Grid x:Name="PART_Title">
<Thumb x:Name="headerThumb"
Opacity="0"
Background="{x:Null}"
Foreground="{x:Null}"
DragDelta="headerThumb_DragDelta"/>
</Grid>
</Border>
</Window>
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WindowState = System.Windows.WindowState.Maximized;
}
private void headerThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
Left = Left + e.HorizontalChange;
Top = Top + e.VerticalChange;
}
}
I've also overridden MouseLeftButtonDown method and using DragMove() inside but without success. I've also tried to subscribe to thumb's MouseLeftButtonDown and write there DragMove() but without success.
By default, maximized windows cannot be moved, thus Left and Top have no effect. One option would be to register to the Thumb.DragStarted event and check if the window is maximized. If yes, you can set WindowState.Normal and successively update the Left and Top properties.
In code, this would look somewhat like this:
private void Thumb_OnDragStarted(object sender, DragStartedEventArgs e)
{
// If the window is not maximized, do nothing
if (WindowState != WindowState.Maximized)
return;
// Set window state to normal
WindowState = WindowState.Normal;
// Here you have to determine the initial Left and Top values
// for the window that has WindowState normal
// I would use something like the native 'GetCursorPos' (in user32.dll)
// function to get the absolute mouse point on all screens
var point = new Win32Point();
GetCursorPos(ref point);
Left = point - certainXValue;
Top = point - certainYValue;
}
You can learn more about GetCursorPos here.
However, I would strongly advise you to use the WindowChrome class that comes with .NET 4.5 and that was also suggested by Max in the comments. You just have to use the following code and you have the functionality you're asking for:
<Window x:Class="ThumbMaximizedWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350"
Width="525"
WindowStyle="None"
WindowState="Maximized">
<WindowChrome.WindowChrome>
<WindowChrome />
</WindowChrome.WindowChrome>
</Window>

How to create an automatic animated carousel cyling images using C# or XAML?

I have done carousels in web development, but animating them in WPF through XAML or C# is new to me. There are examples on the web, but they either are outdated or not what I am looking for. Even when I play around with source code of other projects, it's not what I hope for.
I want to have images sliding left-to-right (horizontally) automatically. The user cannot interact with the images to stop the sliding. While I can do this manually in a ScrollViewer, the process is manual...
ScrollViewer doesn't have any dependencies for animation. I tried using this to see if it is possible, but the application would always crash. Example I used..
Another attempt I've tried is storing images in a StackPanel, making sure the StackPanel is the width of one of my images, then having DispatcherTimer set to animate the TranslateTransform's X property. But...that didn't go anywhere.
Using a ScrollViewer or StackPanel is not important at all. I just want to have a carousel-like effect automatically transitioning through images. Sort of like THIS
I'm currently using Visual Studio 2012 and 2013, if it helps.
Is there a way to do this?
I' ve prepared exemplary carousel in wpf. You might want to use the code in form of UserControl for instance. As you proposed I prepared carousel with use of StackPanel. Code of my form looks as follows:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Storyboard x:Key="CarouselStoryboard">
<DoubleAnimation
Storyboard.TargetName="CarouselTransform"
Storyboard.TargetProperty="X"/>
</Storyboard>
</Window.Resources>
<Canvas>
<StackPanel Name="Carousel" Orientation="Horizontal">
<StackPanel.RenderTransform>
<TranslateTransform x:Name="CarouselTransform" />
</StackPanel.RenderTransform>
<Button Height="350" Width="525" Content="Page1"/>
<Button Height="350" Width="525" Content="Page2"/>
<Button Height="350" Width="525" Content="Page3"/>
</StackPanel>
<Button Click="Left_Click" Content="Left" HorizontalAlignment="Left" Margin="10,164,0,0" VerticalAlignment="Top" Width="45">
</Button>
<Button Click="Right_Click" Content="Right" HorizontalAlignment="Left" Margin="448,170,0,0" VerticalAlignment="Top" Width="45"/>
</Canvas>
</Window>
The Storyboard element within WindowResources defines animation to be performed. It will change X property of TranslationTransform applied to StackPanel "Carousel" - this will result in animated movement of that panel. 3 buttons within the panel simulates 3 panels of the carousel. At the bottom there are 2 buttons - one for moving left and second for moving right. There are callback methods bounded to them. Code behind of the form looks like that:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private int currentElement = 0;
private void Left_Click(object sender, RoutedEventArgs e)
{
if(currentElement < 2)
{
currentElement++;
AnimateCarousel();
}
}
private void Right_Click(object sender, RoutedEventArgs e)
{
if (currentElement > 0)
{
currentElement--;
AnimateCarousel();
}
}
private void AnimateCarousel()
{
Storyboard storyboard = (this.Resources["CarouselStoryboard"] as Storyboard);
DoubleAnimation animation = storyboard.Children.First() as DoubleAnimation;
animation.To = -this.Width * currentElement;
storyboard.Begin();
}
}
currentElement field holds information which panel is currently being displayed to the user. Method AnimateCarousel actualy starts the animation. It refers to Storyboard defined in Resources and sets its DoubleAnimation To property to the value to which Carousel panel should be moved. Then by calling Begin method on storyboard it performs animation.

How to use WinForms host in XAML file OR put it in Toolbox in WPF project

I've read tutorial how to embed Form in WPF's Window http://tech.pro/tutorial/786/wpf-tutorial-using-winforms-in-wpf I test it and it seems to be easy. But the problem is that I have create WinForm host programmatically in the .cs file. This is inconvienient to me.
I would like to put it in the XAML to have better control over its size. If it is possible I also would like to add WinForms host to toolbox
I am not sure why you would want to do that.. but anyhow because the Winform is a top-level control.
public partial class MainWindow : Window
{
Form1 frm = null;
public MainWindow()
{
InitializeComponent();
frm = new Form1();
frm.TopLevel = false;
}
private void frmMain_Loaded(object sender, RoutedEventArgs e)
{
myHost.Child = frm;
}
}
Happy Coding..
This is the accompany XAML...
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" WindowState="Maximized">
<Grid x:Name="frmMain" Background="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Loaded="frmMain_Loaded">
<DockPanel LastChildFill="True">
<WindowsFormsHost x:Name="myHost" HorizontalAlignment="Stretch" Height="Auto" Margin="0" VerticalAlignment="Stretch" Width="Auto"/>
</DockPanel>
</Grid>
</Window>
Try it out... I assure you it works and..

How do i navigate from one Xaml file to another?

I am very new to XAML and WPF.I have a problem.
I have two files. first.xaml and second.Xaml. There is a button in first.xaml, on click of which it should navigate to second.xaml.How do i achieve this.
This is one way of organising the code:
Your second.xaml should contain your window definiton e.g.:
<Window x:Class="MediaCheckerWPF.AboutBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="About Media Checker" Height="300" Width="400" ResizeMode="NoResize"
Icon="/MediaCheckerWPF;component/Resources/checker.ico"
ShowInTaskbar="False">
<Grid>
...
</Grid>
</Window>
Your first.xaml has the button e.g.:
<Button Height="23" HorizontalAlignment="Right" Name="aboutButton"
VerticalAlignment="Top" Width="23" Click="AboutButton_Click"
Content="{DynamicResource TInformationButton}"
ToolTip="{DynamicResource TInformationButtonTooltip}" Margin="0,0,8,0"/>
Then in the code behind:
private void AboutButton_Click(object sender, RoutedEventArgs e)
{
var about = new AboutBox { Owner = this };
about.Initialise();
about.Show();
}
ChrisF's answer is a good way of popping up a new window in a Windows-style application, except you should not call Initialize that way (it should be called from the constructor).
If you want web-style navigation instead, you should use the Page class instead of Window, along with NavigationService. This also allows your WPF application to run inside an actual web browser.

Categories