Is it possible to bind XAML/C# with HTML using PHP? - c#

So I have a XAML button:
<StackPanel>
<TextBlock HorizontalAlignment="Left" Text="{Binding Version, ElementName=Control}" />
<Button Content="Support" Width="100" Click="HelpSupport_Click" />
</StackPanel>
Which links to some C#
private void HelpSupport_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("support.html");
}
Which takes a user to a webpage.
In my XAML StackPanel above I'm binding the software version into a text block for the user to see. But my question is, is it possible that when the user clicks the button to go to the website, can I somehow bind the text block data to be transferred into the HTML too? So that when the user gets to the webpage the software version is also showing there too? Just wondering if this would be done via PHP or a JSON call

I don't have a lot of recent experience with HTML and PHP, but if my memory doesn't fail me, you could add the text of the TextBlock as a variable at the end of the URL of the webpage (it would look like this www.webpage.com?text=version) and then from PHP use the GET method to get the value of the variable text in your URL.
You can do this with more than one variable, like this www.webpage.com?text=version&var2=hello

Managed to get it, these are the changes made:
private void HelpSupport_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("support.php?version=" + HttpUtility.UrlEncode(Version.ToString()));
}
Which put the number in the URL. So my output in HTML:
<textarea type="text" class="form-control" name="es_version" >
<?php echo $_GET["version"]; ?>
</textarea>

Related

I am not able to find button click even method

I am using Visual Studio 2017 and I am creating a Xamarin page.
I have some controls like label and buttons. That is below:
<StackLayout>
<Label Text="Date and Time Picker"
VerticalOptions="Center"
HorizontalOptions="Center"/>
<DatePicker x:Name="datepicker1" MaximumDate="2/2/2012" MinimumDate="1/1/2002"></DatePicker>
<Button x:Uid="btntime" Text="submit" Clicked="mybtn"></Button>
</StackLayout>
In the button, I am not able to open click event. How can I redirect the button click event?
Not sure if i understand your question correctly
but if you want the function to appear in the code behind go in you xaml, select the Clicked function and hit "F12" it will create it in the code-behind
I think it doesn't generate automatically in new version. You have to write it manually in .cs file.
public void mybtn(Object obj, EventArgs args)
{ // your code }

How to set and get tag in xaml dynamically?

While creating GUI using xaml, I created a textbox with a tag like this:
<TextBox Name="TextBox" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="216,178,143,120" Width="158"
Tag="myTag"/>
Now I want to let the user be able to change this tag. For that, I am looking for a kind of function of form:
TextBox.SetTag( "User Provided Tag" )
So that the tag can be changed into this one:
<TextBox Name="TextBox" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="216,178,143,120" Width="158"
Tag="User Provided Tag"/>
After searching the internet for quite a while, I didn't come up with any practical solution. Could someone help? Thanks.
You can use a binding between two controls. Say a user is allowed to enter a tag value from a TextBox. You just bind the Tag of the second TextBox to a Text property of the first TextBox:
<TextBox Name="enterTagTextBox" />
<TextBox Name="getTagTextBox" Tag="{Binding ElementName=enterTagTextBox, Path=Text}"/>
To test it I added a Button in my XAML:
<Button Height="25" Click="Button_Click_1"/>
In code behind I just retrieve the tag value and display it like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string text = this.getTagTextBox.Tag.ToString();
global::System.Windows.Forms.MessageBox.Show(text);
}
I think your problem may arise from you naming your TextBox, "TextBox". Try giving it a name that does not clash with the class name, like "txtMyTextBox".
Then you can do, txtMyTextBox.Tag = "User Provided Tag";.
Or you can bind to it as PiotrWolkowski suggests.
However, I would like to add, that it seems like there should be a cleaner way of achieving your desired behaviour. With the caveat that I don't know the details of what you are trying to implement.
I strongly suggest creating a ViewModel (see: MVVM Pattern) to hold the data you want users to be able to edit. Then use the bindings in WPF to display the data.
You would need to use the property element usage in order to set the Tag property in Extensible Application Markup Language (XAML) to anything other than an object with a known and built-in type converter, such as a string.

Getting html to display in xaml

I have tried phone:Webbrowser which does not automatically size to the required amount.
I have tried many textbox and richttextbox property extension libraries, I cannot get any to work.
I want to know how people get html into textboxes or richtextboxes in windows phone 8.
Argh, I have spent 2 evenings on this now! doh!
Context:
I am calling an API that is returning html (why oh god why)... I want to bind the returned html to a textbox or richtextbox or if I haveeee to, a phone:webbrowser.
Textbox and richtextbox do not support html.
phone:webbrowser does not adjust its height according to what's inside the document. You can supposedly do it by enabling javascript and calling window.external.Notify() but I couldn't get it to work quite right...
Moving on from the above problem, even if I did get the phone:webbrowser to work, if for test purposes I make the width 500 and height 500, I can see my html string as plain text rather than the webcontrol correctly parsing html... doh!
Just try this way.
your xaml:
<Grid x:Name="ContentGrid" Grid.Row="1">
<phone:WebBrowser HorizontalAlignment="Left" Margin="20,50,0,0" Name="webBrowser1" VerticalAlignment="Top" Height="500" Width="430" />
</Grid>
in your code:
public MainPage()
{
InitializeComponent();
webBrowser1.Loaded += WebBrowser_OnLoaded;
}
private void WebBrowser_OnLoaded(object sender, RoutedEventArgs e)
{
webBrowser1.Navigate(new Uri("readme.htm", UriKind.Relative));
}
Hope it helps

loading pic in xaml web browser.

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:WebBrowser x:Name="browser" IsScriptEnabled="True" Margin="-12,0,-11,0" />
</Grid>
I initialized web browser using this code. Since, my html content is too big, it takes much more to load those html files. Till the html files displayed, web browser is being white color.
It makes me irritate. I need to know can we have any loader pic in web browser. So, the pic displayed until web browser loads the html files ???
I can't verify this at the moment but the approach I would take is to display an image via Image or some alternative, such as a loading message in XAML, and set the initial visibility of this stand-in to visible and the WebBrowser to collapsed.
Implement an event handler for the LoadCompleted event on the WebBrowser and when it is triggered, swap the visibility states to hide the progress/wait message and show the web browser.
It'll look roughly like:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Text="Loading... please wait" Visibility="Visibile" x:Name="loadingMessage"/>
<phone:WebBrowser x:Name="browser" IsScriptEnabled="True" Margin="-12,0,-11,0" LoadCompleted="htmlLoadCompleted" Visibility="Collapsed"/>
</Grid>
// In the code behind - HTML finished loading, swap visibility to show the page
private void htmlLoadCompleted(object sender, NavigationEventArgs e)
{
loadingMessage.Visibility = Visibility.Collapsed;
browser.Visibility = Visibility.Visibile;
}
I have concerns over when/how the WebBrowser starts loading, which is why I'd like to caution I don't have a local environment configured to verify this approach is 100% working. You might need to experiment with this some to get it working but I hope this helps set you on the right path.

Programmatically enable a disabled checkbox in a Windows Phone 7 app

I'm writing my first device-specific application which is a Windows Phone 7 panorama app. I'm currently still busy on the UI as I'm playing around with the features then I stumbled upon a problem that I couldn't fix. You see, I have two checkboxes for some sort of login form. One is a "Remember me" and the other a "Sign me in automatically" checkbox. The action I want is that when I uncheck Remember Me, I would like the Sign in Automatically checkbox to be unchecked and disabled. That I was able to do but the reverse always causes an error. I used to write simple PHP web apps and JavaScript so I have some programming knowledge but C# is fairly new to me.
private void RememberMe_Unchecked(object sender, RoutedEventArgs e)
{
AutoSignIn.IsChecked = false;
AutoSignIn.IsEnabled = false;
}
That one works but this one doesn't:
private void RememberMe_Checked(object sender, RoutedEventArgs e)
{
AutoSignIn.IsEnabled = true;
}
The latter throws a "NullReferenceException was unhandled" error.
My XAML code looks like this:
<CheckBox Content="Remember me" Height="71" Name="RememberMe" Unchecked="RememberMe_Unchecked" Checked="RememberMe_Checked" IsEnabled="True" IsChecked="True" />
<CheckBox Content="Sign me in automatically" Height="71" Name="AutoSignIn" IsEnabled="True" IsChecked="True" />
I've done some research and my approach seem to be wrong but I'm not sure how to make it work.
Without the XAML I can't be 100% sure, but make sure you are not setting the IsChecked property programmatically. When you do so, the IsChecked method will get called once before everything is initialised properly on the page. So while the code posted by Matt works:
<CheckBox Name="AutoSignIn" />
<CheckBox Name="RememberMe" Checked="RememberMe_Checked" Unchecked="RememberMe_Unchecked" />
The following won't (because it tries to reference the AutoSignIn box before the page has finished initializing)
<CheckBox Name="AutoSignIn" />
<CheckBox Name="RememberMe" IsChecked="True" Checked="RememberMe_Checked" Unchecked="RememberMe_Unchecked" />
To fix this, you can set the IsChecked property programmatically instead of in XAML, or there might be some other way around this that someone else can point you to.
Your code works for me.
I used your event handlers with the following XAML:
<CheckBox Name="AutoSignIn" />
<CheckBox Name="RememberMe" Checked="RememberMe_Checked" Unchecked="RememberMe_Unchecked" />
Is the error thrown on the line in RememberMe_Checked? or on something else which is set as a consequence of changing the enabled state of AutoSignIn?
Do you, for instance, have any databindings that could be affecting this?

Categories

Resources