I want to get a time of a freeze frame of a video which is paused by the pause button in WPF. I want to store time of that freeze frame and use it in the future. How do i get a perfect time of that specific frame? What type of variable i should use to store that time?
I'd use the Position property, which returns a TimeSpan:
XAML
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<MediaElement x:Name="MyMediaElement"
Grid.Row="0"
LoadedBehavior="Manual"
Source="SomeDrive:\SomeFolder\SomeMediaFile.mp4" />
<StackPanel Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Horizontal">
<Button Width="50"
Height="50"
Click="PlayMedia"
Content="Play" />
<Button Width="50"
Height="50"
Click="PauseMedia"
Content="Pause" />
<TextBlock x:Name="MyTextBlock"
FontSize="30"
Text="0" />
</StackPanel>
</Grid>
Codebehind
private void PlayMedia(object sender, RoutedEventArgs e)
{
MyMediaElement.Play();
}
private void PauseMedia(object sender, RoutedEventArgs e)
{
MyMediaElement.Pause();
var currentTime = MyMediaElement.Position;
MyTextBlock.Text = currentTime.ToString();
}
I believe you are after the Clock property of the MediaElement.
See the documentation.
You can access the CurrentTime property. See more documentation.
Related
I show 4 button, at Row=0/Column=0, Row=0/Column=1, Row=1/Column=0, and Row=1/Column=1, I want to see 3 buttons equally taking. Im doing in Xamarin Forms Platform
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Text="Change Layout1" ></Button>
<Button Grid.Row="1" Text="Change Layout2" ></Button>
<Button Grid.Row="0" Grid.Column="1" Text="Change Layout3" ></Button>
<Button Grid.Row="1" Grid.Column="1" Text="Change Layout4" ></Button>
</Grid>
On Dynamic Change i want to rearrange the grid when one button is removed,
private void Button_Clicked(object sender, EventArgs e)
{
grid.Children.RemoveAt(1);
}
Can anyone suggest your ideas
When a button is clicked, I remove one of the grid items, and then need to rearrange the remaining items automatically. To achieve this, I’m using SetRowSpan() and SetRow().
private void Button_Clicked(object sender, EventArgs e)
{
grid.Children.RemoveAt(1);
Grid.SetRowSpan(button4,2);
}
Hello I am working with windows phone 8.1[RT] application , I simply navigate page only . but I found new option we can use Frame in xaml also like this
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Background="White">
</Border>
<Button Content="next" Click="Button_Click" Background="Black" />
<Grid Grid.Row="1">
<Frame x:Name="Page1Frame" Background="Black" >
<StackPanel>
<Rectangle Height="100" Width="100" Fill="Red" Margin="5" />
<Rectangle Height="100" Width="100" Fill="Red" Margin="5" />
<Rectangle Height="100" Width="100" Fill="Red" Margin="5" />
<Rectangle Height="100" Width="100" Fill="Red" Margin="5" />
</StackPanel>
</Frame>
</Grid>
</Grid>
and navigate this frame like this
private void Button_Click(object sender, RoutedEventArgs e)
{
Page1Frame.Navigate(typeof(BlankPage1));
}
in this example my 120 height grid remain same and just navigate the frame .
I just want to know which is best practice to use ?
Thank you.
Page is page, Frame is frame, they are different.
Suppose your current page named MainPage, if you want to stay in MainPage and change the content of grid in root grid's row 1, you should use:
Page1Frame.Navigate(typeof(BlankPage1));
If you want to leave MainPage to go to another page, you should use:
var rootFrame = Window.Current.Content as Frame;
rootFrame.Navigate(typeof(BlankPage1));
and in this case, what you see is a blank page without 120 height grid remain.
i have a problem , i have 2 uc that displayed in main windows.
i want when i press on button in the first uc the text in the other uc will change
this the axml i use and the code
first uc
<Grid.RowDefinitions>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Width="40" Height="40" Name="playbtn" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" Click="playbtn_Click" >
<Button.Content>
<Image Source="/img/player_play.png" ></Image>
</Button.Content>
</Button>
<Button Width="40" Height="40" Name="pausebtn" Grid.Column="1" Grid.Row="0" >
<Button.Content>
<Image Source="/img/player_pause.png" ></Image>
</Button.Content>
</Button>
<Button Width="40" Height="40" Name="stopbtn" Grid.Column="2" Grid.Row="0" >
<Button.Content>
<Image Source="/img/player_stop.png" ></Image>
</Button.Content>
</Button>
</Grid>
</StackPanel>
</Grid>
</StackPanel>
secound uc <TextBlock Name="Progresstimertext" Text="00:00:00" FontSize="20"></TextBlock>
so i want to press on start button and the timer will be change? how to
10x
This should be managed in the View Model of your Main Window, as this seems the only location that has knowledge of both controls and it sounds as though each of your user control's shouldn't necessarily have knowledge of each other.
Ideally, you should have a reference to each of your user controls in the code-behind (or view model class is you have a dedicated one).
If in code behind, you would simply expose these by giving them names.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="your controls reference here">
<Grid>
<Grid.ColumnDefinitions>
</ColumnDefinition>
</ColumnDefinition>
</Grid.ColumnDefinitions>
<controls:UserControl1 x:Name="Control1"/>
<controls:UserControl2 x:Name="Control2" Grid.Column="1"/>
</Grid>
</Window>
Now you can reference them in your code behind by name Control1 and Control2. These user control classes should be exposing the properties that you want to control, in your case ProgressTimerText, such that, in the code-behind, you can set it easily such as
Control2.ProgressTimerText = "00:00:00";
Where to do this? Well you probably want to create a Stopped event on your Control1, that you can attach to your code behind - in your case, something like Stopped. In your UserControl1 class you should declare something like
public event EventHandler Stopped;
And then in the local event handler for the Click of the stop button, invoke that event.
private void Stop_Click(object sender, RoutedEventArgs e)
{
if (Stopped != null)
Stopped(this, e);
}
Attach that in your MainWindow:
<controls:UserControl1 x:Name="Control1" Stopped=Control1_Stopped/>
Now in your code-behind you should have something like:
private void Control1_Stopped(object sender, RoutedEventArgs e)
{
Control2.ProgressTimerText = "00:00:00";
}
I have a problem trying to develop a WPF page. I have a window with a WPF Frame, when the window is loaded i use MainFrame.Navigate(new page object). The only problem is i can not press any button or use a textbox. Any idea, how can i solve this?
here is the code of my wpf window:
<Window x:Class="ViewLayer.Forms.Win_LoginCloseable"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Win_LoginCloseable" Height="477" Width="501" WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="180*" />
<RowDefinition Height="164*" />
<RowDefinition Height="35*" />
</Grid.RowDefinitions>
<Rectangle Grid.RowSpan="3" Name="Rect_Main" />
<TextBlock Grid.Row="2" FontFamily="Calibri" FontSize="17" FontStyle="Italic" Margin="10,0,12,7" Name="tb_remainding" Text="" TextAlignment="Justify" TextWrapping="WrapWithOverflow" Height="28" VerticalAlignment="Bottom" />
<Button Content="Cerrrar" Grid.Row="1" Height="73" HorizontalAlignment="Center" Name="btn_cancel" VerticalAlignment="Bottom" Width="173" Click="btn_cancel_Click" Background="#FFC70000" Margin="114,0,114,5" />
<Frame x:Name="MainFrame" IsHitTestVisible="False" NavigationUIVisibility="Automatic" />
<TextBlock FontFamily="Calibri" FontSize="22" FontWeight="Bold" Foreground="#FF797979" Height="95" Margin="0,0,0,0" Name="textBlock2" Text="Una vez identificado, luego de 90 segundos de inactividad el sistema cerrará su sesión automaticamente" TextAlignment="Center" TextTrimming="None" TextWrapping="Wrap" VerticalAlignment="Top" Grid.Row="1" HorizontalAlignment="Center" />
<TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="34,100,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
the constructor of the window
private Win_LoginCloseable()
{
InitializeComponent();
this.Pages = new List<Page>();
this.Pages.AddRange(new Page[]{
new MagneticCardPage(),
new UserInputPage()
});
}
here when i load the form:
public void LoadForm(int Index = 0)
{
this.MainFrame.Navigate(this.Pages[Index]);
this.ShowDialog();
}
i repeat, in a page i have textboxs and buttons. But when i try to use them or clicked i can. The events are not getting to the pages.
Thanks in advance
Your code in the constructor is wrong. It should be marked public, instead of private.
The constructor will always call InitializeComponent, but it can't function normally if your constructor is marked as private. Therefore, the control will be displayed BUT the event handle will not be executed, because event handler code is available inside InitializeComponent, and I'm sure it won't be executed.
Change this:
private Win_LoginCloseable()
{
InitializeComponent();
this.Pages = new List<Page>();
this.Pages.AddRange(new Page[]{
new MagneticCardPage(),
new UserInputPage() });
}
Into this:
public Win_LoginCloseable()
{
InitializeComponent();
this.Pages = new List<Page>();
this.Pages.AddRange(new Page[]{
new MagneticCardPage(),
new UserInputPage() });
}
Well! I Figure out the solution of the problem.
here:
<Frame x:Name="MainFrame" IsHitTestVisible="False" NavigationUIVisibility="Automatic" />
i replace it with:
<Frame x:Name="MainFrame" IsManipulationEnabled="True" />
and it works well!
Thanks!
I have a usercontrol in my wpf application which is causing a stackoverflowexception to be caught when it is instanced more than once. I tried to debug the cause of the exception and it is raised during InitializeComponent of my Usercontrol. When I enter InitializeComponent it jumps over to the app.xaml.cs codebehind and reads values which are contained in the Settings class.
I am "new" to using C# application settings so I have not experienced this error before. Not sure if this is commonplace or not when working with them. Also, this is the only usercontrol currently in my app that allows modification of the settings variables and it is the only usercontrol which exhibits this behavior.
I think my problem has something to do with the DebugOptions class using a datacontext of "Application.Current" and then I create another instance with that same datacontext but as soon as I access any of its properties I get the application confused about which obj is which. While that makes sense in my head, logically it doesn't work out that way because this usercontrol is instanced upon a button click and it's host panel is cleared before adding to prevent multiple instances from rolling around.
Posted below is the xaml and codebehind of my usercontrol. It has no dependencies except for the CLR properties from the App class that it binds to. I wish I had more info to provide on this but it's a very odd exception that creeps up.
Here is the property in my App class which causes the stackoverflow exception when it is "Get" accessed.
private Byte _debuglevel = Merlin.Properties.Settings.Default.DebugLevel;
public Byte DebugLevel
{
get { return _debuglevel; }
set { _debuglevel = value; }
}
public partial class DebugOptions : UserControl
{
public DebugOptions()
{
InitializeComponent();
}
private void ChangeLogDirectoryButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
MessageBox.Show("Make a decision here...choose to use the old winforms folder browser control or find one on the web because the std openfiledialog can't be overriden to select folders only.", "Fix this..");
}
private void UpdateDebugOptionsButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
//update debug level
Merlin.Properties.Settings.Default.DebugLevel = (Byte)DebugLevelSlider.Value;
//update log boolean
if ((bool)EnableLoggingRadioButton.IsChecked)
{
Merlin.Properties.Settings.Default.LogsEnabled = true;
}
else
{
Merlin.Properties.Settings.Default.LogsEnabled = false;
}
//update log path?
//save "settings"
Merlin.Properties.Settings.Default.Save();
//write a log event noting changes
App myappreference = (App)Application.Current;
Merlin.Helper.logger.pLogToFile(string.Format("Log Settings Updated at {0} by {1}", DateTime.Now.ToString(), myappreference.CurrentUser.UserName));
}
private void OpenLogDirectoryButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
Process process = new Process();
Process.Start("Explorer.exe", Merlin.Properties.Settings.Default.LogsDirectory);
}
}
Usercontrol resources and UserControl tags omitted for brevity
<Border BorderBrush="Black" BorderThickness="1" Margin="0">
<Grid DataContext="{Binding Source={x:Static Application.Current}}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Content="Debug Options" HorizontalAlignment="Center" Margin="0" Grid.Row="0" VerticalAlignment="Center" FontSize="29.333" Style="{StaticResource UserControlTitleLabelStyle}" />
<StackPanel Orientation="Horizontal" Grid.Row="1">
<Label Content="Set Debug Level" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Slider x:Name="DebugLevelSlider" HorizontalAlignment="Left" VerticalAlignment="Center" Maximum="10" Value="{Binding DebugLevel}" Minimum="1" Margin="62,0,0,0" TickPlacement="BottomRight" SmallChange="1" Style="{StaticResource SliderStyle1}" Width="119">
<Slider.ToolTip>
<ToolTip Content="{Binding DebugLevel}" ContentStringFormat="{}The current value is {0} out of 10"/>
</Slider.ToolTip>
</Slider>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="2">
<Label Content="Application Level Logging: " />
<RadioButton x:Name="EnableLoggingRadioButton" GroupName="Logs" Content="Enable" Margin="5" IsChecked="{Binding LogsEnabled}">
<RadioButton.ToolTip>
<TextBlock Text="Selecting this option will enable logs at the debug level selected above."/>
</RadioButton.ToolTip>
</RadioButton>
<RadioButton x:Name="DisableLoggingRadioButton" GroupName="Logs" Content="Disable" Margin="5" IsChecked="{Binding Path=IsChecked,ElementName=EnableLoggingRadioButton, Converter={StaticResource oppositebooleanconverter}}" >
<RadioButton.ToolTip>
<TextBlock Text="Selecting this option will disable all logs for the application."/>
</RadioButton.ToolTip>
</RadioButton>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="3">
<Label Content="Log Path" HorizontalAlignment="Left" VerticalAlignment="Center" />
<TextBox Margin="10" Width="347.553" TextWrapping="Wrap" Text="{Binding LogsDirectory}" VerticalAlignment="Stretch" />
<StackPanel Height="100">
<Button x:Name="OpenLogDirectoryButton" Content="Open Directory" Width="100" Margin="0,10,0,0" VerticalAlignment="Center" HorizontalAlignment="Right" Style="{StaticResource ButtonStyle}" d:LayoutOverrides="GridBox" Click="OpenLogDirectoryButton_Click" />
<Button x:Name="ChangeLogDirectoryButton" Content="Change Directory" Width="100" Margin="0,10,0,0" VerticalAlignment="Center" HorizontalAlignment="Right" Style="{StaticResource ButtonStyle}" d:LayoutOverrides="GridBox" Click="ChangeLogDirectoryButton_Click" IsEnabled="False" />
</StackPanel>
</StackPanel>
<Button x:Name="UpdateDebugOptionsButton" Content="Update" Grid.Row="4" Width="100" VerticalAlignment="Center" HorizontalAlignment="Right" Style="{StaticResource ButtonStyle}" Click="UpdateDebugOptionsButton_Click" Margin="0,0,8,10" />
</Grid>
</Border>
Stacktrace BEFORE exception thrown
Merlin.exe!Merlin.App.LogsEnabled.set(bool value = false) Line 52 C#
[External Code]
Merlin.exe!Merlin.View.DebugOptions.DebugOptions() Line 25 + 0x8 bytes C#
[External Code]
Merlin.exe!Merlin.View.TestView.TestView() Line 24 + 0x8 bytes C#
Merlin.exe!Merlin.MainWindow.SidebarButtonsClickHandler(object sender = {Merlin.ImageButton}, System.Windows.RoutedEventArgs e = {System.Windows.RoutedEventArgs}) Line 218 + 0x15 bytes C#
[External Code]
What's odd is that during the initializecomponent routine the "LogsEnabled" boolean value is gotten and then immediately it calls to set it. I have no idea what's calling it to set it. But as soon as it sets the value it tries to get it again. I'm sure the runtime is throwing the stackoverflow to prevent an infinite loop. So how can I figure out why it wants to do this?
The stack overflow is probably as a result of a circularly-defined reference: it looks like one way this might happen is that your slider control is bound to DebugLevel. However, when you enter the update method, it defines the value of the DebugLevel to that of the slider control.
So you might get something like:
Slider control's value? Oh - I'll go look up DebugLevel.
DebugLevel's value? Oh, I'll go look up slider control's value.
Slider control's value? Oh - I'll go look up DebugLevel.
I'm not certain, but that could be the problem.
(like the above commenter mentioned, a stack trace would be really helpful here)