I ran into a curious problem: method ScrollTo doesn't work for the ListView inside root page initialized inside NavigationPage. The simple code:
public partial class MainPage : ContentPage
{
ListView listView;
public MainPage()
{
var abs = new AbsoluteLayout { };
var src = new List<string>();
for (int i = 0; i < 30; i++) { src.Add(i + 1 + " line"); }
abs.Children.Add(listView = new ListView { ItemsSource = src }, new Rectangle(0, 0, 1, 0.9), AbsoluteLayoutFlags.All);
abs.Children.Add(new Entry { BackgroundColor = Color.Gray }, new Rectangle(0, 1, 1, 0.1), AbsoluteLayoutFlags.All);
Content = abs;
}
protected override void OnAppearing()
{
base.OnAppearing();
var last = (listView.ItemsSource as List<string>).Last();
listView.ScrollTo(last, ScrollToPosition.End, false);
}
}
Here I try to scroll to the last element when the MainPage is loading. And that works fine if I has initialized App so:
public App ()
{
InitializeComponent();
MainPage = new App1.MainPage { Title = "Main" };
}
But if I put the MainPage inside NavigationPage, scrolling does not occur:
public App ()
{
InitializeComponent();
MainPage = new NavigationPage(new App1.MainPage { Title = "Main" });
}
What is the correct way to solve this?
PS: now I outflank this moment through Device.StartTimer, but I think this is wrong crutch
Related
I want to improve the size and background of my content page on ios,
var About = new ContentPage() { Title = "About" };
var layout = new StackLayout();
var line1 = new Label() { Text = viewModel.Member.Line1, FontSize = 16, HorizontalTextAlignment = TextAlignment.Center };
var MapTab = new ContentPage() {Title = "Map"};
Android:
The title of the content page appears very small on ios and not visible. I need help in trying to improve the looks and make it bigger.
You'll have to implement Custom Renderer for your TabbedPage. See this link:
Extending TabbedPage in Xamarin Forms.
It says, that:
To do these customizations we will use a custom renderer on each platform to render the TabbedPage.
I will duplicate the code snippets from the source as a example:
YourTabbedPage.xaml:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CustomTabbedPage.MainPage"
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
android:TabbedPage.ToolbarPlacement="Bottom">
<TabbedPage.Children>
<ContentPage Title="Home" Icon="ic_home.png" BackgroundColor="White"/>
<ContentPage Title="Favorites" Icon="ic_favorite.png" BackgroundColor="White"/>
<ContentPage Title="App" Icon="app_logo_unselected.png" x:Name="home" BackgroundColor="White"/>
<ContentPage Title="Friends" Icon="ic_people.png" BackgroundColor="White"/>
<ContentPage Title="Settings" Icon="ic_settings.png" BackgroundColor="White"/>
</TabbedPage.Children>
</TabbedPage>
iOS Custom Renderer:
public class ExtendedTabbedPageRenderer : TabbedRenderer
{
public override void ViewWillAppear(bool animated)
{
if (TabBar?.Items == null)
return;
var tabs = Element as TabbedPage;
if (tabs != null)
{
for (int i = 0; i < TabBar.Items.Length; i++)
{
UpdateTabBarItem(TabBar.Items[i], tabs.Children[i].Icon);
}
}
base.ViewWillAppear(animated);
}
private void UpdateTabBarItem(UITabBarItem item, string icon)
{
if (item == null || icon == null)
return;
// Set the font for the title.
item.SetTitleTextAttributes(new UITextAttributes() { Font = UIFont.FromName("GillSans-UltraBold", 12), TextColor = Color.FromHex("#757575").ToUIColor() }, UIControlState.Normal);
item.SetTitleTextAttributes(new UITextAttributes() { Font = UIFont.FromName("GillSans-UltraBold", 12), TextColor = Color.FromHex("#3C9BDF").ToUIColor() }, UIControlState.Selected);
}
}
Android Custom Renderer:
public class ExtendedTabbedPageRenderer : TabbedPageRenderer
{
Xamarin.Forms.TabbedPage tabbedPage;
BottomNavigationView bottomNavigationView;
Android.Views.IMenuItem lastItemSelected;
int lastItemId=-1;
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TabbedPage> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
tabbedPage = e.NewElement as ExtendedTabbedPage;
bottomNavigationView = (GetChildAt(0) as Android.Widget.RelativeLayout).GetChildAt(1) as BottomNavigationView;
bottomNavigationView.NavigationItemSelected += BottomNavigationView_NavigationItemSelected;
//Call to change the font
ChangeFont();
}
if (e.OldElement != null)
{
bottomNavigationView.NavigationItemSelected -= BottomNavigationView_NavigationItemSelected;
}
}
//Change Tab font
void ChangeFont()
{
var fontFace = Typeface.CreateFromAsset(Context.Assets, "gilsansultrabold.ttf");
var bottomNavMenuView = bottomNavigationView.GetChildAt(0) as BottomNavigationMenuView;
for (int i = 0; i < bottomNavMenuView.ChildCount; i++)
{
var item = bottomNavMenuView.GetChildAt(i) as BottomNavigationItemView;
var itemTitle = item.GetChildAt(1);
var smallTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(0));
var largeTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(1));
lastItemId = bottomNavMenuView.SelectedItemId;
smallTextView.SetTypeface(fontFace, TypefaceStyle.Bold);
largeTextView.SetTypeface(fontFace, TypefaceStyle.Bold);
//Set text color
var textColor = (item.Id == bottomNavMenuView.SelectedItemId) ? tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarSelectedItemColor().ToAndroid() : tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarItemColor().ToAndroid();
smallTextView.SetTextColor(textColor);
largeTextView.SetTextColor(textColor);
}
}
void BottomNavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
var normalColor = tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarItemColor().ToAndroid();
var selectedColor = tabbedPage.On<Xamarin.Forms.PlatformConfiguration.Android>().GetBarSelectedItemColor().ToAndroid();
if(lastItemId!=-1)
{
SetTabItemTextColor(bottomNavMenuView.GetChildAt(lastItemId) as BottomNavigationItemView, normalColor);
}
SetTabItemTextColor(bottomNavMenuView.GetChildAt(e.Item.ItemId) as BottomNavigationItemView, selectedColor);
this.OnNavigationItemSelected(e.Item);
lastItemId = e.Item.ItemId;
}
void SetTabItemTextColor(BottomNavigationItemView bottomNavigationItemView, Android.Graphics.Color textColor)
{
var itemTitle = bottomNavigationItemView.GetChildAt(1);
var smallTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(0));
var largeTextView = ((TextView)((BaselineLayout)itemTitle).GetChildAt(1));
smallTextView.SetTextColor(textColor);
largeTextView.SetTextColor(textColor);
}
}
I created a content class for the two pages I wanted to improve on, the map and the memberaboutpage and I instead of using the content page, I did this
var About = new MemberAboutPage { Title = "About" };
var layout = new StackLayout();
var MapTab = new MapPage() { Title = "Map" };
Then I added the pages to the pages I created and mirrored to the ios rendere page below, this page formats the tabs and makes them more nicer looking and alos prevents the overlapping on iPhone X. Happy Programming Mates
'[assembly: ExportRenderer(typeof(CardPage), typeof(MyiOSTabbedPage))]
[assembly: ExportRenderer(typeof(LoginPage), typeof(MyiOSTabbedPage))]
[assembly: ExportRenderer(typeof(MemberAboutPage), typeof(MyiOSTabbedPage))]
[assembly: ExportRenderer(typeof(MapPage), typeof(MyiOSTabbedPage))]
namespace CHA.iOS.Renderers
{
public class MyiOSTabbedPage : PageRenderer
{
public override void ViewWillLayoutSubviews()
{
base.ViewWillLayoutSubviews();
nfloat tabSize = 44.0f;
UIInterfaceOrientation orientation = UIApplication.SharedApplication.StatusBarOrientation;
CGRect rect = this.View.Frame;
rect.Y = this.NavigationController != null ? tabSize : tabSize + 20;
this.View.Frame = rect;
if (TabBarController != null)
{
CGRect tabFrame = this.TabBarController.TabBar.Frame;
tabFrame.Height = tabSize;
tabFrame.Y = this.NavigationController != null ? 0 : 0;
this.TabBarController.TabBar.Frame = tabFrame;
this.TabBarController.TabBar.BarTintColor = UIColor.FromRGB(234,232,232);
var textAttr = new UITextAttributes
{
Font = UIFont.SystemFontOfSize(20)
};
var selectedAttr = new UITextAttributes
{
TextColor = UIColor.FromRGB(63,165,186),
Font=UIFont.BoldSystemFontOfSize(20)
};
foreach (var i in this.TabBarController.TabBar.Items)
{
i.SetTitleTextAttributes(textAttr, UIControlState.Normal);
i.SetTitleTextAttributes(selectedAttr, UIControlState.Selected);
}
}
}
}'
I am trying to add a click event to a button that is defined in a different class.
Just a quick overview, this code creates a stack of cards with buttons on them and the buttons need click events.
The CardView class:
public class CardView : ContentView
{
public Label Name { get; set; }
public Image Photo { get; set; }
public Label Location { get; set; }
public Label Description { get; set; }
public Button PassButton { get; set; }
public Button FailButton { get; set; }
public CardView()
{
// gives the card its black line
Grid grid = new Grid();
grid.BackgroundColor = Color.Black;
grid.Padding = 2;
RelativeLayout view = new RelativeLayout();
// box view as the background
BoxView boxView1 = new BoxView
{
Color = Color.White,
InputTransparent = true
};
view.Children.Add(boxView1,
Constraint.Constant(0), Constraint.Constant(0),
Constraint.RelativeToParent((parent) => {
return parent.Width;
}),
Constraint.RelativeToParent((parent) => {
return parent.Height;
})
);
// items image
Photo = new Image()
{
InputTransparent = true,
Aspect = Aspect.Fill
};
view.Children.Add(Photo,
Constraint.Constant(0),
Constraint.RelativeToParent((parent) =>
{
double h = parent.Height * 0.80;
return ((parent.Height - h) / 2) + 20;
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Width;
}),
Constraint.RelativeToParent((parent) =>
{
return (parent.Height * 0.40);
})
);
// items label
Name = new Label()
{
TextColor = Color.Black,
FontSize = 22,
InputTransparent = true,
FontAttributes = FontAttributes.Bold,
HorizontalOptions = LayoutOptions.CenterAndExpand,
HorizontalTextAlignment = TextAlignment.Center
};
view.Children.Add(Name,
Constraint.Constant(10), Constraint.Constant(10),
Constraint.RelativeToParent((parent) =>
{
return parent.Width;
}),
Constraint.Constant(28)
);
// location description
Location = new Label()
{
TextColor = Color.Black,
FontSize = 18,
InputTransparent = true
};
view.Children.Add(Location,
Constraint.Constant(30), Constraint.Constant(40),
Constraint.RelativeToParent((parent) =>
{
return parent.Width;
}),
Constraint.Constant(28)
);
//Image[] stars = new Image[5];
StackLayout stack = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Spacing = 2
};
view.Children.Add(stack,
Constraint.RelativeToParent((parent) =>
{
return parent.Width - 90; //
}),
Constraint.Constant(40)); //40
// bottom label
Description = new Label()
{
TextColor = Color.Black,
FontSize = 16,
FontAttributes = FontAttributes.None,
HorizontalOptions = LayoutOptions.CenterAndExpand,
HorizontalTextAlignment = TextAlignment.Center,
InputTransparent = true
};
view.Children.Add(
Description,
Constraint.Constant(0),
Constraint.RelativeToParent((parent) =>
{
return (parent.Height / 2f) + 30;
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Width;
}),
Constraint.Constant(40)
);
// camera button
Button camera = new Button()
{
Text = "Camera",
InputTransparent = true
};
view.Children.Add(camera,
Constraint.RelativeToParent((parent) =>
{
return (parent.Width / 2f) - (camera.Width / 2f);
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Height - 70;
})
);
PassButton = new Button()
{
Text = "Pass",
};
view.Children.Add(PassButton,Constraint.RelativeToParent((parent)=>
{
return (parent.Width / 8f) - (PassButton.Width / 4f);
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Height - 70;
})
);
FailButton = new Button()
{
Text = "Fail",
};
view.Children.Add(FailButton, Constraint.RelativeToParent((parent) =>
{
return (parent.Width *(3/ 4f)) - (FailButton.Width / 4f);
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Height - 70;
})
);
grid.Children.Add(view);
Content = grid;
}
}
This class is used as sort of a template for the entire deck.
In another class multiple instances of the CardView class are created to make the deck of cards.
The creation of the cards is working fine.
So in the CardStackView (in the constructor) the deck is created:
public CardStackView()
{
RelativeLayout view = new RelativeLayout();
// create a stack of cards
for (int i = 0; i < NumCards; i++) //
{
var card = new CardView();
cards[i] = card;
card.InputTransparent = true;
card.IsVisible = false;
view.Children.Add(
card,
Constraint.Constant(0),
Constraint.Constant(0),
Constraint.RelativeToParent((parent) =>
{
return parent.Width;
}),
Constraint.RelativeToParent((parent) =>
{
return parent.Height;
})
);
}
this.BackgroundColor = Color.Azure;
this.Content = view;
}
Is it possible to add click events/tapgestures to the buttons in the CardView class (PassButton and FailButton) in the constructor of the CardStackView when the deck is being created?
They kind of need to be added here given the nature of the rest of the CardStackView. There is a lot going on and its not feasible to reorganise the code at this point.
Is it even possible to do this?
Also things I have tried.
I tried using the instance of the CardView in the for loop to access the button and add the events but this simply did nothing.
i.e card.PassButton.Clicked += clickEvent.
Any ideas? Or even if it is possible?
Here we go.
I think that buttons, labels and image inside the CardView can be private variables, so you can deal with it in a more controlled way (the outside class should not change the position of the button inside your card, for example)
There's a lot of ways to do that. I thought 2 ways given your code structure:
- 1 Encapsulating the event handler definition
You can create a method in your CardView like:
public class CardView : ContentView
{
// Your stuffs
public void AddPassButtonClickedEvent(EventHandler handler)
{
if(handler != null)
PassButton.Clicked += handler;
}
// Your stuffs
}
Outside the class, you will use it like:
var cardView = new CardView();
cardView.AddPassButtonClickedEvent((sender, args) =>
{
// Do something
});
- 2 Exposing your own event
You can create your own events (or commands) at the CardView class and always asign the clicked event of the button. Like this:
public class CardView : ContentView
{
// Your stuffs
public event EventHandler MyPassButtonClickedEvent;
public CardView()
{
// Instantiate your PassButton
PassButton.Clicked += OnPassButtonClicked;
}
protected void OnPassButtonClicked(object sender, EventArgs args)
{
MyPassButtonClickedEvent?.Invoke(object, args);
}
// Your stuffs
}
Again, you can use it outside this way:
class Fake
{
CardView cardView;
public Fake()
{
cardView = new CardView();
cardView.MyPassButtonClickedEvent += MyHandler;
}
void MyHandler(object sender, EventArgus args)
{
// Do something
}
}
In Xamarin.Forms I need to create a popup window that shows login page in popup window.
here is my code using xlab popup control.
MainPage l = new MainPage();
Navigation.PushModalAsync(l);
PopupLayout popupLayout = new PopupLayout();
popupLayout.Content = l.Content;
ShowPopup(l);
MainPage extends ContentPage and currently its working fine for login screen, but my requirement is to show it as a popup. Can anyone please help on this? Or is there any other way to do this ?
Here is how you do it
private async void ShowPopup()
{
//Create `ContentPage` with padding and transparent background
ContentPage loginPage = new ContentPage
{
BackgroundColor = Color.FromHex("#D9000000"),
Padding = new Thickness(20, 20, 20, 20)
};
// Create Children
//Create desired layout to be a content of your popup page.
var contentLayout = new StackLayout
{
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Vertical,
Children =
{
// Add children
}
};
//set popup page content:
loginPage.Content = contentLayout;
//Show Popup
await Navigation.PushModalAsync(loginPage, false);
}
#misho thanks for your help
here is my final working code. But it can't be called popup window. This can just fulfill my purpose.
private async void ShowPopup()
{
ContentPage detailsPage = new ContentPage
{
BackgroundColor = Color.Transparent,// Color.FromHex("#00F0F8FF"),
Padding = new Thickness(40, 40, 40, 40)
};
MainPage l = new MainPage();
detailsPage.Content = l.Content;
Button b = l.FindByName<Button>("btnClose");
b.Clicked += ((o2, e2) =>
{
this.Navigation.PopModalAsync();
});
await Navigation.PushModalAsync(detailsPage, false);
}
Hi #Misho here is actual screen shot op popup screen.
I have a Xamarin.Forms (1.4.2.6359) Project using Visual Studio 2013 and have created the carousel page below. I want to add page indicators, i.e. dots over top of the carousel page. Is this able to be done with the Xamarin Forms CarouselPage?
public class SplashPage : CarouselPage
{
public SplashPage ()
{
this.Children.Add(new CarouselChild("Logo.png", "Welcome"));
this.Children.Add(new CarouselChild("Settings.png", "Settings"));
}
}
class CarouselChild : ContentPage
{
public CarouselChild(string image, string text)
{
StackLayout layout = new StackLayout
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
};
layout.Children.Add(new Image
{
Source = image,
});
layout.Children.Add(new Label
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.EndAndExpand,
Text = text,
Scale = 2,
});
this.Content = layout;
}
}
Trying to keep things simple, what I did was:
MyCarouselPage:
class MyCarouselPage : CarouselPage
{
private int totalPages;
private int currentPage;
public MyCarouselPage()
{
var pages = GetPages();
totalPages = pages.Length;
this.ChildAdded += MyCarouselPage_ChildAdded;
for (int i = 0; i < totalPages; i++)
{
currentPage = i;
this.Children.Add(pages[i]);
}
}
void MyCarouselPage_ChildAdded(object sender, ElementEventArgs e)
{
var contentPage = e.Element as MyPageBase;
if (contentPage != null)
{
contentPage.FinalStack.Children.Add(new CarouselPageIndicator(currentPage, totalPages, "indicator.png", "indicator_emtpy.png"));
}
}
private MyPageBase[] GetPages()
{
var pages = new MyPageBase[] { new Page1(), new Page2() };
return pages;
}
}
The Base class for the Pages
class MyPageBase:ContentPage
{
public StackLayout FinalStack { get; set; }
}
CarouselPageIndicator
public class CarouselPageIndicator : StackLayout
{
public CarouselPageIndicator(int currentIndex, int totalItems, string sourceIndicator, string souceEmptyIndicator)
{
this.Orientation = StackOrientation.Horizontal;
this.HorizontalOptions = LayoutOptions.CenterAndExpand;
for (int i = 0; i < totalItems; i++)
{
var image = new Image();
if (i == currentIndex)
image.Source = sourceIndicator;
else
image.Source = souceEmptyIndicator;
this.Children.Add(image);
}
this.Padding = new Thickness(10);
}
}
And for the n-Pages
class Page1:MyPageBase
{
public Page1()
{
var layout = new StackLayout
{
Children = {
new Label{Text="Page 1"}
}
};
this.FinalStack = layout;
this.Content = FinalStack;
}
}
I was able to make a work around for the problem by hard coding the page indicators by changing CarouselChild method below:
public CarouselChild(string image, string text, int pageNumber, int pageCount)
{
var width = this.Width;
StackLayout layout = new StackLayout
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Padding = new Thickness( 40, 40, 40, 40),
BackgroundColor = Color.Black,
};
layout.Children.Add(new Image
{
Source = image,
VerticalOptions = LayoutOptions.Start,
HorizontalOptions = LayoutOptions.Center,
});
layout.Children.Add(new Label
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
Text = text,
FontSize = 36,
LineBreakMode = LineBreakMode.WordWrap,
});
layout.Children.Add(CarouselPageIndicator(pageNumber, pageCount));
this.Content = layout;
}
internal StackLayout CarouselPageIndicator(int pageNumber, int pageCount)
{
StackLayout layout = new StackLayout
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.EndAndExpand,
};
if (pageCount >= pageNumber)
{
for (int i = 1; i < pageCount + 1; i++)
{
if (i == pageNumber)
{
layout.Children.Add(new Image
{
Source = "Light.png",
});
}
else
{
layout.Children.Add(new Image
{
Source = "Dark.png",
});
}
}
}
return layout;
}
I want to change content of button1 on click event of button2 . But not able to get the object of grid's child Button class which is in List<> UiList.
Please Guide me in getting the right approach to look it and solve it . And also guide that if the object is build in runtime then how to access it ?
public partial class MainPage : PhoneApplicationPage
{
List<Grid> UIList = new List<Grid>();
Grid objGrid1 = null;
Button objButton1 = null;
Button objButton2 = null;
// Constructor
public MainPage()
{
InitializeComponent();
createGrid1("grid1");
createButton2("Button2");
}
public void createGrid1(string x)
{
objGrid1 = new Grid();
objGrid1.Height = 100;
objGrid1.Name = x;
objGrid1.Width = 200;
objGrid1.Margin = new Thickness(100, 100, 0, 0);
objGrid1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objGrid1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objGrid1.Background = new SolidColorBrush(Colors.Orange);
createButton1("changename");
}
public void createButton1(string _name)
{
objButton1 = new Button();
objButton1.Height = 90;
objButton1.Name = _name;
objButton1.Content="Button1";
objButton1.FontSize = 20;
objButton1.Width = 190;
objButton1.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objButton1.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objButton1.Background = new SolidColorBrush(Colors.Blue);
objButton1.Foreground = new SolidColorBrush(Colors.White);
objGrid1.Children.Add(objButton1);
LayoutRoot.Children.Add(objGrid1);
UIList.Add(objGrid1);
}
public void createButton2(string _name)
{
objButton2 = new Button();
objButton2.Margin = new Thickness(240, 300, 0, 0);
objButton2.Name = _name;
objButton2.Height = 90;
objButton2.Content = "Button2";
objButton2.FontSize = 20;
objButton2.Width = 190;
objButton2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
objButton2.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objButton2.Background = new SolidColorBrush(Colors.Blue);
objButton2.Foreground = new SolidColorBrush(Colors.White);
LayoutRoot.Children.Add(objButton2);
objButton2.Click += (s, e) =>
{
int c = UIList.ElementAt(0).Children.Count;
if (c == 1)
{
//logic to change content of Button1 on click of Button2
}
};
}
}
Assuming you cannot just keep a reference to the created control, such as in a class field, you can iterate through the Children property of the Grid and find the desired Button. If there are multiple buttons, you can differentiate them using the Tag property.
Once you find it, change the contents of the button using the Content property, as discussed in the contents above.