I have a MediaElement, but how can I call a function when the property "position" of MediaElement changes?
Position is not a DependencyProperty.
You can use a DispatchTimer. This article provides some good insight on how to get this working. MediaElement and More with WPF.
Here is some sample code that I took from a project I'm working on. It shows the position of the video using a slider control and allows the user to change the position.
I'm a bit of a newbie too, so it is possible that some of it is wrong (feel free to comment on problems in the comments section :).
private DispatcherTimer mTimer;
private bool mIsDragging = false;
private bool mTick = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
medPlayer.Play();
medPlayer.Stop();
mTimer = new DispatcherTimer();
mTimer.Interval = TimeSpan.FromMilliseconds(100);
mTimer.Tick += new EventHandler(mTimer_Tick);
mTimer.Start();
}
void mTimer_Tick(object sender, EventArgs e)
{
if (!mIsDragging)
{
try
{
mTick = true;
sldPosition.Value = medPlayer.Position.TotalMilliseconds;
}
finally
{
mTick = false;
}
}
}
private void sldPosition_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
mIsDragging = true;
medPlayer.Pause();
}
private void sldPosition_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
mIsDragging = false;
if (chkPlay.IsChecked.Value)
medPlayer.Play();
}
private void sldPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var pos = TimeSpan.FromMilliseconds(e.NewValue);
lblPosition.Content = string.Format("{0:00}:{1:00}", pos.Minutes, pos.Seconds);
if (!mTick)
{
medPlayer.Position = TimeSpan.FromMilliseconds(sldPosition.Value);
if (medPlayer.Position == medPlayer.NaturalDuration.TimeSpan)
{
chkPlay.IsChecked = false;
medPlayer.Stop();
}
}
}
Related
I have prepared more than one UserControl for a windows program in XAML. Each user control works as a separate page. But I do page transitions in navigate class. In ".xaml.cs" when calling User control
Navigate.navigate (navigate_grid, new DeviceLayout ());
I'm using the line of code. But every time I create a new user control, the background functions don't work. How do I flip one instead of invoking a new user control each time?
class Navigate
{
public static void navigate(Grid grd, UserControl uc)
{
if (grd.Children.Count > 0)
{
grd.Children.Clear();
grd.Children.Add(uc);
}
else { grd.Children.Add(uc); }
}
}
Example navigate:
public SettingsView()
{
InitializeComponent();
Navigate.navigate(navigate_grid, new SystemLayout());
}
private void system_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new SystemLayout());
previous_page.Text = "";
current_page.Text = "SİSTEM";
next_page.Text = "UYGULAMA";
}
private void application_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new ApplicationLayout());
previous_page.Text = "SİSTEM";
current_page.Text = "UYGULAMA";
next_page.Text = "BAĞLANTI";
}
private void connection_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new ConnectionLayout());
previous_page.Text = "UYGULAMA";
current_page.Text = "BAĞLANTI";
next_page.Text = "ÜRÜNLER";
}
private void product_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new ProductsLayout());
previous_page.Text = "BAĞLANTI";
current_page.Text = "ÜRÜNLER";
next_page.Text = "CİHAZLAR";
}
private void device_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new DeviceLayout());
previous_page.Text = "ÜRÜNLER";
current_page.Text = "CİHAZLAR";
next_page.Text = "YAZICILAR";
}
private void printer_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new PrinterLayout ());
previous_page.Text = "CİHAZLAR";
current_page.Text = "YAZICILAR";
next_page.Text = "KULLANICILAR";
}
private void users_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new UsersLayout());
previous_page.Text = "YAZICILAR";
current_page.Text = "KULLANICILAR";
next_page.Text = "BAKIM";
}
private void maintenance_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new MaintenanceLayout());
previous_page.Text = "KULLANICILAR";
current_page.Text = "BAKIM";
next_page.Text = "HAKKINDA";
}
private void info_button_click(object sender, RoutedEventArgs e)
{
Navigate.navigate(navigate_grid, new InfoLayout());
previous_page.Text = "BAKIM";
current_page.Text = "HAKKINDA";
next_page.Text = "";
}
}
Not sure if I get what you mean.
When you change the UserControl with the use of any of those functions eg info_button_click you can't access this funtion anymore.
That would be the case, because your XAML and .cs file are one class, containing those funcitons. If you change the UserControl (XAML) you will also change the .cs file. Therefore you can't access those functions anymore.
You could probably get the behaviour you want if you bind the commands to a viewmodel, which you could then pass through the navigation as well?
Sry, I'm still not sure what exactly it is you're doing.
Hello is there something similar to AllowNavigation like in WinForms?
My search didn't yield any satisfying result.
Basically I'm trying to open a webpage inside new wpf window and stop user from clicking random links on that webpage and navigating further.
Saw something with
void browser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}
Rest of the code is :
public Popup_webpage(string ime)
{
InitializeComponent();
browser1.LoadCompleted += browser1_LoadCompleted;
browser1.Navigating += browser1_Navigating;
string uri = "www.google.com"
browser1.Navigate(new Uri(uri, UriKind.Absolute));
}
void browser1_LoadCompleted(object sender, NavigationEventArgs e)
{
browser1.Visibility = Visibility.Visible;
}
But it just makes my webpage not display ?
Thanks
Try this:
Once you have loaded your page, then you assign the browser_Navigating event handler.
public Popup_webpage(string ime)
{
InitializeComponent();
browser1.LoadCompleted += browser1_LoadCompleted;
string uri = "www.google.com"
browser1.Navigate(new Uri(uri, UriKind.Absolute));
browser1.Navigating += browser1_Navigating;
}
void browser1_LoadCompleted(object sender, NavigationEventArgs e)
{
browser1.Visibility = Visibility.Visible;
}
void browser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}
I am developing an analog clock picker control.
The user is able to click on the minute or hour hand and drag to turn the needle to select the specific time. I was wondering how to detect such a click and drag event.
I tried using MouseLeftButtonDown + MouseMove but I cannot get it to work as MouseMove is always trigger when the mousemove happen despite me using a flag. Is there any easier way?
public bool dragAction = false;
private void minuteHand_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
dragAction = true;
minuteHand_MouseMove(this.minuteHand, e);
}
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (dragAction == true)
{
//my code: moving the needle
}
}
private void minuteHand_MouseLeftButtonUp(object sender, MouseEventArgs e)
{
dragAction = false;
}
I think this is the easiest and most straightforward way :
private void Window_MouseMove(object sender, MouseEventArgs e) {
if (e.LeftButton == MouseButtonState.Pressed) {
this.DragMove();
}
}
public bool dragAction = false;
private void minuteHand_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
dragAction = true;
minuteHand_MouseMove(this.minuteHand, e);
}
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (dragAction == true)
{
this.DragMove();
}
}
private void minuteHand_MouseLeftButtonUp(object sender, MouseEventArgs e)
{
dragAction = false;
}
does the trick
You can make things easier and need not handle mouse down / up :
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
//my code: moving the needle
}
}
I have a quick question: I need to switch between the two camera on a tablet. Front and Back. By default, the Front camera is always used by Emgu CV.
Thanks.
Ok. There is a different constructor. I was building upon the 7 line demo for Emgu CV.
Using the correct overloaded constructor, this is what did the trick for me:
private Capture _capture;
private void InitCapture(Int32 _camIndex) {
try {
if (_capture != null) {
Application.Idle -= ProcessFrame;
}
_capture = new Capture(_camIndex);
Application.Idle += ProcessFrame;
}
catch (NullReferenceException excpt) {
XtraMessageBox.Show(excpt.Message);
}
}
private void ProcessFrame(object sender, EventArgs arg) {
Image<Bgr, Byte> frame = _capture.QueryFrame();
ImageBoxCapture.Image = frame;
}
private void CharmsBarBase_ButtonTop01Click(object sender, EventArgs e) {
InitCapture(0);
}
private void CharmsBarBase_ButtonTop02Click(object sender, EventArgs e) {
InitCapture(1);
}
Regards.
I have a problem again. when I click button, window appears. when I click button again same window appears again. I want when I click button first time, page appears. but I want to prevent second click. can anyone help me with this problem? thanks in advance.
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
}
set a simple boolean value to check if the window is already open?
private bool isWindowAlreadyOpen = false;
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
if (!isWindowAlreadyOpen)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
isWindowAlreadyOpen = true;
}
}
Should do the trick.
EDIT
You'll have to register the closed event of the window to unset the boolean:
private bool isWindowAlreadyOpen = false;
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
if (!isWindowAlreadyOpen)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
dic.Closed += Dictionary_Closed;
isWindowAlreadyOpen = true;
}
}
private void Dictionary_Closed(object sender, EventArgs e)
{
isWindowAlreadyOpen = false;
}
EDIT2
Alternatively, you can use dic.ShowDialog() if you want this window to be topmost and only one instance.