Xamarin Forms Launcher - visual-studio

I'm working on a little project where users get match with certain events (ex: Soccer, Tennis, etc). Now I implemented a button that takes the user to a discord chat. The issue I'm having trouble figuring out is, I want each event to have its own specific button link that takes the user to the specific discord chat within the server and I'm not sure how to do that can someone help me. This is what I have so far.
protected async void DiscordChat_Tapped(object sender, EventArgs e)
{
await Launcher.OpenAsync("https://discord.gg/nGHTS56F");
}
Thanks in advance.

place the Discord ID in the ClassId property
<Button ClassId="SomeID" Clicked="DiscordChat_Tapped" ... />
<Button ClassId="SomeOtherID" Clicked="DiscordChat_Tapped" ... />
then in your handler
protected async void DiscordChat_Tapped(object sender, EventArgs e)
{
var btn = (Button)sender;
var id = btn.ClassId;
await Launcher.OpenAsync($"https://discord.gg/{id}");
}
alternately, if your Button is inside a template
protected async void DiscordChat_Tapped(object sender, EventArgs e)
{
var btn = (Button)sender;
// cast the BindingContext to the correct type
var item = (MyClass)btn.BindingContext;
// then use some property from that type
await Launcher.OpenAsync($"https://discord.gg/{item.SomeProperty}");
}

Related

Button x:Name="btnBack" not navigating to previous page

I am developing Xamarin Forms application for IOS and Android.I have the list of 3 xamarin form pages say X.xaml, Y.xaml, Z.xaml. Page X.xaml, Y.xaml is having 1 and click on that button it opens the next-next Xamarin form. And last on Z.xaml having 2 buttons btnBack & btnConfirm
<Button x:Name="btnBack"></Button>
<Button x:Name="btnConfirm"></Button>
when I click on that back button I want to navigate it to my previous page, but I don't want it like Navigation Back Button or Hardware Back Button click.
for this I have tried this code this will not work.
Z.xaml.cs
public partial class Z : ContentPage
{
public Z()
{
InitializeComponent();
btnBack.Clicked += btnBack_Clicked;
}
private void btnBack_Clicked(object sender, EventArgs e)
{
var existingPages = Navigation.NavigationStack.ToList();
foreach (var page in existingPages)
{
if(page == this)
Navigation.RemovePage(page);
}
}
}
In this code when I click on Back Button it Navigate me to prevoius page ie Y.xaml once I click on button which is on Y.xaml and open the Z.xaml page it shows me blank.
Y.xaml.cs
public partial class Y: ContentPage
{
public Y()
{
InitializeComponent();
btnZ.Clicked += btnZ_Clicked;
}
void btnZ_Clicked(object sender, System.EventArgs e)
{
Navigation.PushAsync(new Z());
}
}
#Kowalski had the right answer however it is incomplete.
You have to await Navigation.PopAsync() otherwise you may mess up the navigation stack. So always await it. Example:
async void btnBack_Clicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
Beside that be aware that there are bugs in Xamarin.Forms related to navigation, here is one that might be interesting for you as well:
https://bugzilla.xamarin.com/show_bug.cgi?id=59172
P.S.: Here you can find the official guide on Popping Pages from the Navigation Stack.
you should invoke Navigation.PopAsync() to go back

How to get notified if we move to next page in CarouselView?

I would like to get notified if I move to the next page in CarouselView in Xamarin.Forms.
Is it possible?
Any event or property or method available?
Thanks in advance.
CarouselView has an ItemSelected event that can be subscribe to:
PizzDeals.ItemSelected += (object sender, SelectedItemChangedEventArgs e) =>
{
System.Diagnostics.Debug.WriteLine(e.SelectedItem);
var pizza = e.SelectedItem as SpecialtyPizzaDeals;
System.Diagnostics.Debug.WriteLine(pizza.name);
};

Why am I getting a Null reference exception for a Slider value changed event that happens each time the page loads

I am new to creating Windows Phone 7 Apps, I have added a slider to my page to which i have customized the look but when i load the page in the emulator before the page even loads I get a "NullReferenceException" I thought this was because I had not initialized the slider so i changed the settings method to
public settings()
{
InitializeComponent();
sldPassLegnth.Value = (double)3;
}
The value changed event simply looks like this:
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
double d;
d = sldPassLegnth.Value;
}
The xaml for the slider is:
<Slider Style="{StaticResource SliderStyle1}" Margin="24,75,22,352" Name="sldPassLegnth" ValueChanged="Slider_ValueChanged" Background="Black" Foreground="#FF3399FF" Maximum="15" Minimum="3" />
Any insight into this would be great! Thanks in advance.
Try this instead:
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
double d;
d = e.NewValue;
}

Hyperlink button website

I need to put button that after click moves user to website. Can someone tell me what should I put into vb and in xaml?
<HyperlinkButton Content="Click here to learn about Silverlight" NavigateUri="http://www.silverlight.net" TargetName="_blank" />
You can do it also in C#
private void HyperlinkButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var task = new Microsoft.Phone.Tasks.WebBrowserTask
{
URL = "http://siteaddress.com",
};
task.Show();
}

Sharepoint 2010 - Problem with event handlers for dynamically created controls

In a WebPart using Sharepoint 2010, I´ve got a problem when creating dynamically a LinkButton because his event isn´t fired.
My code is:
Default.aspx
<asp:TextBox ID="formAccountCode" runat="server" MaxLength="8" Columns="8"></asp:TextBox>
<asp:Table ID="idTabela" runat="server" Width="100%" BorderWidth="1px" GridLines="Both"></asp:Table>
<asp:Button ID="btPesquisar" runat="server" onclick="btPesquisar_Click" Text="Pesquisar" />
Default.aspx.cs
protected void btPesquisar_Click(object sender, EventArgs e)
{
LinkButton lkButton = new LinkButton();
lkButton.Text = "Teste Tabela";
lkButton.ID = "link1";
lkButton.Attributes.Add("runat", "server");
lkButton.CommandArgument = "Codigo 1";
lkButton.Command += test;
TableRow tr;
TableCell td1;
td1 = new TableCell();
td1.Controls.Add(lkButton);
tr = new TableRow();
tr.Cells.Add(td1);
idTabela.Rows.Add(tr);
idTabela.DataBind();
}
protected void test(object sender, EventArgs e)
{
formAccountCode.Text = "HI"; // just for test
}
The idea is return a select from DB and create a result with linkbutton to each record returned.
These LinkButtons will be created after an action from the user, because this, it can´t be created in OnInit.
They will connect with another web part.
please move the code for dynamically created button and event handler in CreateChildControls() and call the EnsureChildControls() in Onload
For example:
protected override void CreateChildControls()
{
base.CreateChildControls();
// Create our drop down list, but don't populate it yet
_dropDownList = new DropDownList();
this.Controls.Add(_dropDownList);
}

Resources