I need help from someone who work in C#. I'm trying to achieve WPF application that open internet explorer on URL that i supplement him as a parameter. So in perfect world i would like run something like this:
\\path\window.exe X x Y "URL" "Title"
Where X,Y is windows height and width, URL and Title is a title of that window. My knowledge of C# is nonexistent so after extensive use of google i manage to get this XAML:
<Window x:Class="WPFWebControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="My Web Browser" WindowState="Normal" Loaded="Window_Loaded" WindowStyle="ThreeDBorderWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="Auto" Width="Auto">
<Grid>
<WebBrowser Height="Auto" HorizontalAlignment="Left" Margin="0,28,0,0" Name="MyWebBrowser" VerticalAlignment="Top" Width="Auto" LoadCompleted="MyWebBrowser_LoadCompleted" />
<TextBox Height="23" Margin="40,5,0,0" Name="MyTextBox" VerticalAlignment="Top" HorizontalAlignment="Left" Width="708" />
<Button Content="Go" Margin="10,5,0,476" Name="MyGo" ToolTip="Go" Click="MyGo_Click" RenderTransformOrigin="-0.76,0.565" HorizontalAlignment="Left" Width="25" />
</Grid>
</Window>
and this C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFWebControl
{ /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
MyWebBrowser.Source = new Uri("http://www.google.com");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void MyGo_Click(object sender, RoutedEventArgs e)
{
try
{
MyWebBrowser.Source = new Uri("http://" + MyTextBox.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void MyWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{}
}
}
Now i need some advice how can i set window size, url and title via parameters so i can get rid of that address textbox and button.
You could open a new window and set a WebBrowser as its Content, e.g.:
WebBrowser browser = new WebBrowser();
Window win = new Window();
win.Content = browser;
win.Height = 100;
win.Width = 100;
win.Show();
win.Title = "title...";
browser.Navigate("http://google.com");
If you want your application to be able to accept command line arguments, you could remove the StartupUri attribute from your App.xaml and override the OnStartup method of your App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
try
{
double width = Convert.ToDouble(e.Args[0]);
double height = Convert.ToDouble(e.Args[1]);
string title = e.Args[2];
string url = e.Args[3];
WebBrowser browser = new WebBrowser();
Window win = new Window();
win.Content = browser;
win.Height = height;
win.Width = width;
win.Show();
win.Title = title;
browser.Navigate(new Uri(url, UriKind.Absolute));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Usage:
WpfApplication1.exe 100 100 "title..." "http://google.com"
You should be able to use
string[] args = Environment.GetCommandLineArgs();
Or, change teh startup uri in App.xaml (remove it) and instead create your own startup like
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
//Manually create and show your window, passing the arguments
}
}
etc
Related
I'm using Material design. I created a color picker to choose the color the user wants, after the user chooses the color and theme.
I want to save these settings into a text file on the disk. I don't know how can I convert these types to a list for the string which can I use for reading theme that is saved :
private void MyColorPicker1_PreviewMouseMove(object sender, MouseEventArgs e)
{
string filepath = #"C:\Themses";
if (e.LeftButton == MouseButtonState.Pressed)
{
ITheme theme = _paletteHelper.GetTheme();
theme.SetPrimaryColor(Color.FromRgb(MyColorPicker1.Color.R, MyColorPicker1.Color.G, MyColorPicker1.Color.B)); //red
var Test = theme.GetBaseTheme();
// something here to write all setting inside of ITheme into the text file
//
_paletteHelper.SetTheme(theme);
}
}
How can I do that?
Full XAML:
<Window x:Class="WpfApp5.SettingThemsWins.MaterialThemSettingy"
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"
xmlns:local="clr-namespace:WpfApp5.SettingThemsWins"
mc:Ignorable="d"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
Background="{DynamicResource MaterialDesignPaper}"
Title="Setting" Height="607" Width="1144" WindowStartupLocation="CenterScreen">
<Grid>
<materialDesign:ColorPicker x:Name="MyColorPicker1" HorizontalAlignment="Left" Margin="20,17,0,0" VerticalAlignment="Top" Height="353" Width="750" PreviewMouseMove="MyColorPicker1_PreviewMouseMove" />
<ToggleButton x:Name="ThemeActivationsBtn" Style="{StaticResource MaterialDesignSwitchToggleButton}" ToolTip="Activation Of Dark Theme" IsChecked="False" Margin="110,380,0,0" Click="ThemeActivationsBtn_Click" HorizontalAlignment="Left" Width="63" Height="27" VerticalAlignment="Top" />
<Label Content="Dark Theme :" HorizontalAlignment="Left" Height="24" Margin="20,382,0,0" VerticalAlignment="Top" Width="85"/>
<Button x:Name="SaverThemy" Content="Save Theme" HorizontalAlignment="Left" Margin="200,375,0,0" VerticalAlignment="Top" Width="170" Click="SaverThemy_Click"/>
</Grid>
</Window>
Code behind:
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApp5.SettingThemsWins
{
/// <summary>
/// Interaction logic for MaterialThemSettingy.xaml
/// </summary>
public partial class MaterialThemSettingy : Window
{
private readonly PaletteHelper _paletteHelper = new PaletteHelper();
bool isDark;
public MaterialThemSettingy()
{
InitializeComponent();
//EmptySampleWind.Window1 window1 = new EmptySampleWind.Window1();
//window1.Show();
}
public static IEnumerable<string> SortByLength(IEnumerable<string> e)
{
// Use LINQ to sort the array received and return a copy.
var sorted = from s in e
orderby s.Length ascending
select s;
return sorted;
}
private void MyColorPicker1_PreviewMouseMove(object sender, MouseEventArgs e)
{
string filepath = #"C:\Themses";
if (e.LeftButton == MouseButtonState.Pressed)
{
ITheme theme = _paletteHelper.GetTheme();
theme.SetPrimaryColor(Color.FromRgb(MyColorPicker1.Color.R, MyColorPicker1.Color.G, MyColorPicker1.Color.B)); //red
var Test = theme.GetBaseTheme();
// something here to write all setting inside of ITheme into the text file
//
_paletteHelper.SetTheme(theme);
}
}
private void ThemeActivationsBtn_Click(object sender, RoutedEventArgs e)
{
isDark = (bool)ThemeActivationsBtn.IsChecked;
if (isDark)
{
ITheme theme = _paletteHelper.GetTheme();
IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme();
theme.SetBaseTheme(baseTheme);
_paletteHelper.SetTheme(theme);
}
else
{
ITheme theme = _paletteHelper.GetTheme();
IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme();
theme.SetBaseTheme(baseTheme);
_paletteHelper.SetTheme(theme);
}
}
}
}
I'm trying to correctly remove a UIElement from an InlineUIContainer in order to use it in another Panel but the program keeps crashing with this message "Specified Visual is already a child of another Visual or the root of a CompositionTarget.".
I've created a small application to illustrate my pain. In this program, once Randy the button is killed\deleted by his girlfriend, he doesn't still detach from his parent, whom I got find out was UIElementIsland. And then any attempt to add Randy as the child of anything else crashes the application (The Apocalypse Button proves my point :) ). You can click to check Randy's parents before\after deleting Randy to notice that he is constantly under UIElementIsland as a child, If he is detached the whole problem\apocalypse should be averted.
It's a Funny application so copy and compile even if it's just for the fun! Any help\ideas would be appreciated!
THE C# Part:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace DetachingfromUIElementIsland
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
int t = 0;
static string[] info = new string[] { "Okay, Lets have a look...", "Checking."
, "Checking..", "Checking...", "Seen it!" };
/// <summary>
/// Makes the App fancy :)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void timer_Tick(object sender, EventArgs e)
{
display.Text = info[t];
if (t == 0)
timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
t++;
if (t >= 4)
{
t = 0;
timer.Stop();
display.Text = GetRandysParent();
}
}
private void deleteRandy_Click(object sender, RoutedEventArgs e)
{
// This might be the bug.
// Maybe there's a better way to do this.
// If there was a VisualTreeHelper.Remove().
randy_container.Child = null;
display.Text = "Haha! I just killed Randy!!! He'll never get the chance"
+ "\n to hurt another woman again!";
display.Background = Brushes.Violet;
end.Visibility = System.Windows.Visibility.Visible;
}
DispatcherTimer timer = null;
/// <summary>
/// Check if Randy is Still attached to UIElementIsland
/// </summary>
/// <returns></returns>
private string GetRandysParent()
{
// Check the visual tree to see if randy is removed properly
DependencyObject dp = VisualTreeHelper.GetParent(randy);
string text = string.Empty;
if (dp != null)
{
display.Background = Brushes.LightGreen;
text = "Randy's Dad is Mr " + dp.ToString();
}
else
{
// This should be what you'll get when the code works properly
display.Background = Brushes.Red;
text = "Weird...Randy doesn't seem to have a dad...";
}
return text;
}
private void findParents_Click(object sender, RoutedEventArgs e)
{
display.Background = Brushes.Yellow;
// Creates a timer to display some fancy stuff
// and then Randy's.
// Just to prove to you that this button actually works.
timer = new DispatcherTimer();
timer.Start();
timer.Tick += timer_Tick;
timer.Interval = new TimeSpan(0, 0, 0, 0, 700);
}
private void randy_Click(object sender, RoutedEventArgs e)
{
// Get Randy to introduce himself
display.Text = "Hi, I'm Randy!!!";
display.Background = Brushes.Orange;
}
private void end_Click(object sender, RoutedEventArgs e)
{
// If randy is removed properly, this would not crash the application.
StackPanel s = new StackPanel();
s.Children.Add(randy);
// CRASH!!!
}
}
}
The XAML:
<Window x:Class="DetachingfromUIElementIsland.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">
<FlowDocument IsEnabled="True" x:Name="document">
<Paragraph>
<InlineUIContainer x:Name="randy_container">
<!--Meet Randy-->
<Button Name="randy" Content="I am a Randy, the button" Click="randy_Click" ToolTip="Meet Randy"/>
</InlineUIContainer>
<LineBreak/>
<LineBreak/>
<InlineUIContainer x:Name="container2">
<!--Meet Randy's Ex Girlfriend-->
<Button Name="deleteRandy" Content="Randy dumped me for another girl :(, click me to delete him" Click="deleteRandy_Click" ToolTip="Meet Randy's Ex Girlfriend"/>
</InlineUIContainer>
<LineBreak/>
<LineBreak/>
<InlineUIContainer x:Name="container3">
<!--He can help you find Randy's Parents-->
<Button Name="findParents" Content="Click me to find randy's parents" Click="findParents_Click" ToolTip="He can help you find Randy's Parents"/>
</InlineUIContainer>
<LineBreak/>
<LineBreak/>
<InlineUIContainer x:Name="Apocalypse">
<!--End the world, Crash the application-->
<Button x:Name="end" Content="Avenge Randy's Death" Click="end_Click" ToolTip="End the world, Crash the application" Visibility="Hidden"/>
</InlineUIContainer>
</Paragraph>
<Paragraph>
<InlineUIContainer>
<TextBlock x:Name="display" Foreground="Black"/>
</InlineUIContainer>
</Paragraph>
</FlowDocument>
</Window>
The whole code was supposed to be shorter than this, but I spiced it up to make it a bit fun. Hope I brightened someone's day a little. But still, help me :).
Answer:
Derive from Randy's InlineUIContainer as follows:
public class DerivedInlineUIContainer : InlineUIContainer
{
public DerivedInlineUIContainer()
{
}
public void RemoveFromLogicalTree(FrameworkElement f)
{
this.RemoveLogicalChild(f);
}
}
Now you could kill Randy properly this time, and add him to UIElement heaven (The StackPanel):
randy_container.RemoveFromLogicalTree(randy);
IDisposable disp = VisualTreeHelper.GetParent(randy) as IDisposable;
if (disp != null)
disp.Dispose();
// Poor Randy is going to heaven...
StackPanel heaven = new StackPanel();
heaven.add(randy);
Thanks everyone.
Removing the visual parent doesn't seem to help:
private void end_Click(object sender, RoutedEventArgs e)
{
IDisposable disp = VisualTreeHelper.GetParent(randy) as IDisposable;
if (disp != null)
disp.Dispose();
DependencyObject parent = VisualTreeHelper.GetParent(randy);
if (parent == null)
MessageBox.Show("No parent");
// If randy is removed properly, this would not crash the application.
StackPanel s = new StackPanel();
s.Children.Add(randy);
}
So you could either create a new Button:
public MainWindow()
{
InitializeComponent();
randy_container.Child = CreateRandyButton();
}
private void end_Click(object sender, RoutedEventArgs e)
{
StackPanel s = new StackPanel();
s.Children.Add(CreateRandyButton());
}
private Button CreateRandyButton()
{
Button button = new Button { Name = "randy", Content = "I am a Randy, the button", ToolTip = "Meet Randy" };
button.Click += randy_Click;
return button;
}
...or simply hide it as suggested by #Sinatr.
It's funny, but also very noisy. You would get answer much faster if your demo is short.
Instead of removing/adding visual you can simply hide/show it:
void deleteRandy_Click(object sender, RoutedEventArgs e) =>
randy.Visibility = Visibility.Hidden;
void end_Click(object sender, RoutedEventArgs e) =>
randy.Visibility = Visibility.Visible;
This way you are not playing with visual tree in unrecoverable way. You can use MVVM + data templates or x:Shared=False resources if you really want to remove UI element and then add new one.
I found a workaround in case the parent is still a UIElementIsland. Since it implements IDisposable, you can clear its children that way:
var parent = VisualTreeHelper.GetParent(element);
if (parent is IDisposable uiElementIsland)
{
uiElementIsland.Dispose();
}
It's not nice, but it works.
can not make it work, whatever I tried so far.
I want to make a Remote Camera Commander application (WPF/C#) in Visual Studio 2015 that consists of a number buttons that, when clicked, do a web request to a network camera. I am not a programmer, so I am starting from scratch.
Have searched the internet for many days now and tested many examples, but when inserting a piece of example code into my code, always new issues arise.
I have made an example that hopefully explains my issues:
MainWindow.xaml:
<Window x:Class="RCC_1v1.MainWindow"
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"
xmlns:local="clr-namespace:RCC_1v1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Content="GetCameraType_Button" Click="GetType" HorizontalAlignment="Left" Margin="44,108,0,0" VerticalAlignment="Top" Width="168" Height="86"/>
<RichTextBox x:Name="response" HorizontalAlignment="Left" Height="78" VerticalAlignment="Top" Width="169" Margin="246,112,0,0" TextChanged="response_TextChanged">
<FlowDocument>
<Paragraph>
<Run Text ="How to get the response from webclient into this textbox, instead of in the messagebox??"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
MainWindow.xaml.cs:
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
namespace RCC_1v1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private void GetType(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("stackO", "12345");
wc.DownloadStringAsync(new Uri("http://eremote-cam1.eu.ngrok.io/axis-cgi/param.cgi?action=list&group=root.Brand"));
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
}
private void response_TextChanged(object sender, TextChangedEventArgs e)
{
}
}
}
You can run it yourself with the temp. the account that is embedded. Not sure if I should use TextBox or any other object like listview or.
The expected responses are text-string and single numbers.
Any tip, direction or help is appreciated.
Just followed some of your suggestions, code is now:
enter code hereusing System;
using System.Net;
using System.Windows;
namespace RCC_1v2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private void GetType(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("stackO", "54321");
wc.DownloadStringAsync(new Uri("http://eremote-cam1.eu.ngrok.io/axis-cgi/param.cgi?action=list&group=root.Brand.ProdFullName"));
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
this.Dispatcher.BeginInvoke(new Action(() => {
String run_text = e.Result.ToString();
Run1.Text = run_text.Substring(24, 13);
}));
}
}
}
enter image description here
Give the name "Run1" to the Run contained in your XAML using x:Name attribute.
Replace your code:
MessageBox.Show(e.Result.ToString());
with this one:
this.Dispatcher.BeginInvoke(new Action(() => {
this.Run1.Text = e.Result.ToString();
}));
The event DownloadStringCompleted runs in a background thread, so if you want to update the UI you must use the Dispatcher property of the Window class.
Ok I just found out about the idea of posting an answer. With help of the posters I created the next example/solution:
using System;
using System.Net;
using System.Windows;
namespace RCC_1v2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private void GetType(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.Credentials = new NetworkCredential("xxx", "xxx");
wc.DownloadStringAsync(new Uri("http://eremote-cam1.eu.ngrok.io/axis-cgi/param.cgi?action=list&group=root.Brand.ProdFullName"));
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
this.Dispatcher.BeginInvoke(new Action(() => {
String run_text = e.Result.ToString();
Run1.Text = run_text.Substring(24, 13);
}));
}
}
}
Mainwindow.xaml:
<Window x:Class="RCC_1v2.MainWindow"
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"
xmlns:local="clr-namespace:RCC_1v2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="button" Content="GetCameraType_Button" Click="GetType" HorizontalAlignment="Left" Margin="44,108,0,0" VerticalAlignment="Top" Width="140" Height="86"/>
<TextBox x:Name="Run1" HorizontalAlignment="Left" Height="78" VerticalAlignment="Top" Width="285" Margin="206,113,0,0" >
</TextBox>
</Grid>
</Window>
As you can see I now have the response in the texbox (and not a RichTextBox)
nb. I was not allowed to post a picture :-(
enter image description here
Thanks!
everyone. I've encountered a problem while developing my test media player in C#. I think I put all the code, but when I open file, it starts playing but progress bar isn't moving. Here is the code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Media;
using Windows.Storage;
using Windows.Storage.Pickers;
using System.Threading;
using System.Windows;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
TimeSpan _position;
private async void Open_Click(object sender, RoutedEventArgs e)
{
var openPicker = new FileOpenPicker();
openPicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
openPicker.FileTypeFilter.Add(".wmv");
openPicker.FileTypeFilter.Add(".mp4");
openPicker.FileTypeFilter.Add(".mp3");
var file = await openPicker.PickSingleFileAsync();
if (file != null)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
videoMediaElement.SetSource(stream, file.ContentType);
}
}
private void Play_Click(object sender, RoutedEventArgs e)
{
if(videoMediaElement.PlaybackRate != 1 )
{
videoMediaElement.DefaultPlaybackRate = 1;
}
videoMediaElement.Play();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
videoMediaElement.Stop();
}
private void ProgressBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
}
private void timer_Tick(object sender, object e)
{
if (videoMediaElement.Source != null && videoMediaElement.NaturalDuration.HasTimeSpan)
{
ProgressBar.Minimum = 0;
ProgressBar.Maximum = videoMediaElement.NaturalDuration.TimeSpan.TotalSeconds;
ProgressBar.Value = videoMediaElement.Position.TotalSeconds;
}
}
private void videoMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
_position = videoMediaElement.NaturalDuration.TimeSpan;
ProgressBar.Minimum = 0;
ProgressBar.Maximum = _position.TotalSeconds;
}
}
}
and XAML:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="Open" Content="Open" HorizontalAlignment="Left" Margin="60,564,0,0" VerticalAlignment="Top" Width="299" Click="Open_Click"/>
<Button x:Name="Play" Content="Play" HorizontalAlignment="Left" Margin="393,564,0,0" VerticalAlignment="Top" Width="299" Click="Play_Click"/>
<Button x:Name="Stop" Content="Stop" HorizontalAlignment="Left" Margin="737,564,0,0" VerticalAlignment="Top" Width="299" Click="Stop_Click"/>
<ProgressBar x:Name="ProgressBar"
HorizontalAlignment="Left" Height="16" Margin="60,665,0,0" VerticalAlignment="Top" Width="1185" ValueChanged="ProgressBar_ValueChanged"/>
<Slider x:Name="slider" HorizontalAlignment="Left" Margin="1082,557,0,0" VerticalAlignment="Top" Width="163"/>
<MediaElement x:Name="videoMediaElement" HorizontalAlignment="Left" Height="491.884" Margin="4.22,58.023,0,0" VerticalAlignment="Top" Width="1270.501" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto" MediaOpened="videoMediaElement_MediaOpened">
<MediaElement.RenderTransform>
<CompositeTransform Rotation="0.111"/>
</MediaElement.RenderTransform>
</MediaElement>
</Grid>
PLZ HELP!! I wasted 2 days and I found no real solution on google. Thank you!
Well I looked at your code and found your problem. It appears that SetSource() method doesn't really set mediaelement's source. I have no idea why. Maybe someone here will help you with that. There are several things that I can point out.
You start filling this progressbar before the movie starts. Which is a problem. You could initialize your timer not in MainPage constructor but in your Play_Click event or in this case where your movie starts automatically in you Open_Click event.
As I mentioned since the source is always null the progressbar will never fill. Fortunately there is a way to check if video element is currently running. Replace this:
if (videoMediaElement.Source != null && videoMediaElement.NaturalDuration.HasTimeSpan)
With This:
if (videoMediaElement.CurrentState == MediaElementState.Playing)
Hope this helps.
In a WPF application, I have a webbrowser called WebBrowser1. This refers to an HTML page which contains a TextArea to which users can input text.
<html>
<body>
<textarea class="myStudentInput" id="myStudentInput1">
Text to be copied
</textarea>
</body>
</html>
I wish to get this text and potentially also set this text.
I have tried something similar to the javascript way of writing it:
document.getElementById("myStudentOutput1").innerHTML;
such as
HtmlElement textArea = webBrowser1.Document.All["myStudentInput1"];
dynamic textArea = WebBrowser1.Document.GetElementsByID("myStudentInput1").InnerText;
but it doesn't work.
The following solution in Visual Studio 2015 WPF Application works for me.
First, add a reference to the Microsoft HTML COM Library. This is on the COM tab, when you do an "Add Reference" in your project.
Then add the code:
<Window x:Class="WpfApplication3.MainWindow"
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"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Grid>
<WebBrowser x:Name="WebBrowser1" HorizontalAlignment="Left" Height="480" Margin="10,10,0,0" VerticalAlignment="Top" Width="770" Source="E:\Others\Dropbox\Programming\Questions.html"/>
<Button x:Name="mySetQuestionButton" Content="Set Question" HorizontalAlignment="Left" Margin="200,520,0,0" VerticalAlignment="Top" Width="75" Click="mySetQuestion"/>
<Button x:Name="myGetAnswerButton" Content="Get Answer" HorizontalAlignment="Left" Margin="350,520,0,0" VerticalAlignment="Top" Width="75" Click="myGetAnswer"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="600,520,0,0" TextWrapping="Wrap" Text="Hello2" VerticalAlignment="Top"/>
</Grid>
</Window>
and
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void mySetQuestion(object sender, EventArgs e)
{
mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
mshtml.IHTMLElement textArea = document.getElementById("myQuestion1");
textArea.innerHTML = "What is 1+1?";
}
private void myGetAnswer(object sender, EventArgs e)
{
mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
mshtml.IHTMLElement textArea = document.getElementById("myStudentInput1");
textBlock.Text = textArea.innerHTML;
}
}
}
but it doesn't work.
I have no idea what that could possibly mean. All you can get is a code snippet that does work:
public partial class Form1 : Form {
private WebBrowser webBrowser1;
private Button button1;
public Form1() {
button1 = new Button { Text = "Test" };
button1.Click += button1_Click;
this.Controls.Add(button1);
webBrowser1 = new WebBrowser { Dock = DockStyle.Fill };
webBrowser1.DocumentText = #"<html><body><textarea class=""myStudentInput"" id=""myStudentInput1"">Text to be copied</textarea></body></html>";
this.Controls.Add(webBrowser1);
}
private void button1_Click(object sender, EventArgs e) {
var elem = webBrowser1.Document.GetElementById("myStudentInput1");
MessageBox.Show(elem.InnerText);
}
}
Which produces: