Capture entry value in click event - c#

I have a entry control added using c# to my xamarin.forms app. I also have a toolbaritem, on click of which I would like to save the data which is being entered by user.
I cannot access the text value in click event, I get this error:
Error 20 The name 'txtTest' does not exist in the current context
Here is my code sample:
public SettingsPage()
{
ToolbarItem Settings = new ToolbarItem();
Settings.Name = "Settings";
Settings.Clicked += OnClick_Settings;
Settings.Order = ToolbarItemOrder.Primaru;
ToolbarItems.Add(Settings);
loadData();
}
async public void loadData()
{
Label lblTest = new Label { Text = "Test", FontAttributes = FontAttributes.Bold };
Entry txtTest = new Entry();
StackLayout stLTest = new StackLayout
{
Padding = new Thickness(10, 0, 0, 0),
Children ={
lblTest,
txtTest
}
};
Content = stTest
}
async private void OnClick_Settings(object sender, EventArgs e)
{
var test= txtTest.Text;
}
In my OnClick_Settings, I cannot find text value.

you have txtTest in loadData() method.
you should move it out of there.
Do this
Entry txtTest;
async public void loadData()
{
Label lblTest = new Label { Text = "Test", FontAttributes = FontAttributes.Bold };
txtTest = new Entry();
StackLayout stLTest = new StackLayout
{
Padding = new Thickness(10, 0, 0, 0),
Children ={
lblTest,
txtTest
}
};
Content = stTest
}

Related

How to implement flashlight button on Android in Xamarin Forms using ZXing Net Mobile

I am looking for a clean way to create a (custom) overlay with a flashlight button for the barcode scanner using the Zxing Net Mobile NuGet package.
I already looked at some samples, but unfortunately these samples use clean Xamarin and no MvvmCross.
The code below is placed inside a custom 'ScannerService', which gets called within a MvxViewModel (ScanViewModel).
What I've got so far:
IScannerService.cs
public class ScannerService : IScannerService
{
public void Scan(Action<Result> onComplete)
{
MobileBarcodeScanner.Initialize(Xamarin.Essentials.Platform.CurrentActivity.Application);
MobileBarcodeScanner scanner = new MobileBarcodeScanner
{
TopText = "Houd uw telefoon voor de barcode.",
FlashButtonText = "Flitser",
CancelButtonText = "Stoppen",
BottomText = "Het scannen gebeurt automatisch."
};
var overlay = new ZxingOverlayView(Xamarin.Essentials.Platform.CurrentActivity.Application);
var button = new Button();
button.Clicked += (sender, e) => scanner.ToggleTorch();
overlay.AddChildrenForAccessibility(button);
scanner.UseCustomOverlay = true;
scanner.CustomOverlay = overlay;
bool result = false;
scanner.ScanContinuously((scanResult) =>
{
if (!result)
{
result = true;
scanner.Cancel();
Device.BeginInvokeOnMainThread(() => onComplete(scanResult));
}
});
}
}
The problem is that I created a Xamarin.Forms Button and that this button can't be added to the custom overlay of the 'ZxingOverlayView'.
ScanViewModel.cs
private async Task Scan()
{
if (await _mediaService.CheckPermissions().ConfigureAwait(false))
{
_scanner.Scan((result) => MvxNotifyTask.Create(() => MainThread.InvokeOnMainThreadAsync(() => HandleScan(result)), OnException));
}
}
Is there an alternative way to add the flashlight button to the custom overlay?
Edit:
As Leo suggested I used the default example of ZXing.Net.Mobile. I added the 'ZXingDefaultOverlay' to my page, but unfortunately everytime I use a ZXing component inside my page it throws the following exception:
Java.Lang.NullPointerException: 'Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference'
Here is my code so far:
ScanningViewModel.cs (BaseViewModel inherits from 'MvxViewModel')
public class ScanningViewModel : BaseViewModel
{
private readonly IMvxNavigationService _navigationService;
private readonly IUserDialogs _userDialogs;
ZXingScannerView ZXingScannerView;
ZXingDefaultOverlay ZXingDefaultOverlay;
private Grid _grid;
public Grid Grid
{
get => _grid;
set => SetProperty(ref _grid, value);
}
public ScanningViewModel(IMvxNavigationService navigationService, IUserDialogs userDialogs)
{
_navigationService = navigationService;
_userDialogs = userDialogs;
ZXingScannerView = new ZXingScannerView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
AutomationId = "zxingScannerView"
};
ZXingScannerView.OnScanResult += (result) =>
Device.BeginInvokeOnMainThread(async () =>
{
// Stop analysis until we navigate away so we don't keep reading barcodes
ZXingScannerView.IsAnalyzing = false;
// Show an alert
await _userDialogs.AlertAsync("Scanned Barcode", result.Text, "OK");
});
ZXingDefaultOverlay = new ZXingDefaultOverlay
{
TopText = "",
BottomText = "",
ShowFlashButton = true,
AutomationId = "zxingDefaultOverlay"
};
ZXingDefaultOverlay.FlashButtonClicked += (sender, e) =>
{
ZXingScannerView.IsTorchOn = !ZXingScannerView.IsTorchOn;
};
var grid = new Grid
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
var stopButton = new Button
{
WidthRequest = 100,
HeightRequest = 50,
HorizontalOptions = LayoutOptions.Start,
VerticalOptions = LayoutOptions.End,
Text = "Disable",
Command = new Command(() => ZXingScannerView.IsScanning = false)
};
var cancelButton = new Button
{
WidthRequest = 100,
HeightRequest = 50,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.End,
Text = "Cancel",
Command = new Command(async () => await _navigationService.Close(this))
};
var startButton = new Button
{
WidthRequest = 100,
HeightRequest = 50,
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.End,
Text = "Enable",
Command = new Command(() => ZXingScannerView.IsScanning = true)
};
grid.Children.Add(ZXingScannerView);
grid.Children.Add(ZXingDefaultOverlay);
grid.Children.Add(startButton);
grid.Children.Add(cancelButton);
grid.Children.Add(stopButton);
// The root page of your application
Grid = grid;
}
//protected override void OnAppearing()
//{
// base.OnAppearing();
// ZXingScannerView.IsScanning = true;
//}
//protected override void OnDisappearing()
//{
// ZXingScannerView.IsScanning = false;
// base.OnDisappearing();
//}
}
ScanningPage.xaml (This is a 'MvxContentPage')
<ContentPage.Content>
<StackLayout>
<ContentView Content="{Binding Grid}"></ContentView>
</StackLayout>
</ContentPage.Content>
ScanningPage.xaml.cs
[MvxContentPagePresentation(WrapInNavigationPage = true)]
public partial class ScanningPage : MvxContentPage<ScanningViewModel>
{
public ScanningPage()
{
InitializeComponent();
}
}
Any idea why it throws this specific error?
I use version 2.4.1 of the 'ZXing.Net.Mobile' and 'ZXing.Net.Mobile.Forms' NuGet package.

How to display a pin on maps

I could display the map on my App interface , and now I'm trying to display a Pin of my current position .
I displayed in but with a button Click that shows me another Content Page . but I couldn't display it on the same page even after clicking that button .
If there's a way to show it , on the same page and without clicking any button , I'll be so thankful .
Here's the code of my Clicked_Button Event , This Code works and when I click it displays a new content page with my current position . but what I want it to to show it in my Map Module in the Main Page,
this is the XAML Code for my map (putting it in a grid):
<maps1:Map Grid.Column="1"
Grid.Row="2"
Grid.RowSpan="2"
Grid.ColumnSpan="3"
x:Name="myMap">
I tried to remove " Content = myMap; " from my back-end code to keep it on the main page , but the pin doesn't show up.
protected async void OnButtonClicked_speed(object sender, EventArgs args)
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));
myMap = new Xamarin.Forms.Maps.Map(MapSpan.FromCenterAndRadius(
new Xamarin.Forms.Maps.Position(position.Latitude,position.Longitude),
Distance.FromMiles(0.5)))
{
IsShowingUser = true,
VerticalOptions = LayoutOptions.FillAndExpand
};
var position1 = new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude);
var pin1 = new Pin
{
Type = PinType.Place,
Position = position1,
Label = "Current Position",
Address = ""
};
myMap.Pins.Add(pin1);
Content = myMap;
}
Content = myMap
this is equivalent to repopulating your current page with a map
so you just move the code out of your OnButtonClicked_speed method.
like this:
in MainPage.xaml.cs
public partial class MainPage : ContentPage
{
private Map myMap;
public MainPage(Position position)
{
InitializeComponent();
myMap.MoveToRegion(MapSpan.FromCenterAndRadius(
new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude),
Distance.FromMiles(0.5)));
myMap.IsShowingUser = true;
myMap.VerticalOptions = LayoutOptions.FillAndExpand;
var position1 = new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude);
var pin1 = new Pin
{
Type = PinType.Place,
Position = position1,
Label = "Current Position",
Address = ""
};
myMap.Pins.Add(pin1);
}
public static async Task<MainPage> CreateMainPageAsync()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));
MainPage page = new MainPage(position );
return page;
}
}
in App.xaml.cs
public partial class App : Application
{
public App()
{
InitializeComponent();
}
protected override async void OnStart()
{
namespace.MainPage main = await namespace.MainPage.CreateMainPageAsync();
MainPage = main;
}
}

How to set the background using acrylic brush through C# in UWP?

I've tried the following:
{
ContentDialog dialog = new ContentDialog()
{
Title = title,
Content = text,
CloseButtonText = closeButtonText
};
dialog.Background = new AcrylicBrush()
{
BackgroundSource = 0,
TintOpacity = 0.5,
Opacity = 0.5,
};
await dialog.ShowAsync();
}
P.S. - Sorry for the Russian and my bad English. Thank you in advance đź‘Ť
Your code is well if you can see the button after the content dialog.
The code that I do not use acrylic brush.
private async void Button_OnClick(object sender, RoutedEventArgs e)
{
var title = "title";
var text = "text";
var closeButtonText = "close";
ContentDialog dialog = new ContentDialog()
{
Title = title,
Content = text,
CloseButtonText = closeButtonText
};
dialog.Background = new SolidColorBrush(Color.FromArgb(255, 202, 24, 37));
await dialog.ShowAsync();
}
The code that I use acrylic brush.
private async void Button_OnClick(object sender, RoutedEventArgs e)
{
var title = "title";
var text = "text";
var closeButtonText = "close";
ContentDialog dialog = new ContentDialog()
{
Title = title,
Content = text,
CloseButtonText = closeButtonText
};
if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent(
"Windows.UI.Xaml.Media.XamlCompositionBrushBase"))
{
// check that this API is available on the user’s machine
dialog.Background = new AcrylicBrush()
{
BackgroundSource = Windows.UI.Xaml.Media.AcrylicBackgroundSource.HostBackdrop,
TintOpacity = 0.5,
FallbackColor = Color.FromArgb(255, 202, 24, 37),
Opacity = 0.5,
};
}
await dialog.ShowAsync();
}
See Acrylic material - Windows UWP applications
Customize Acrylic Brush in UWP Applications

Adding Events to MenuItem Dyanamically in C#

I have created menu dynamically.But I don't know how to handle events of menu Item.Please Let me know if anyone have solution.Thanks in Advance.
ToolStripMenuItem master,transaction,report,exit;
private void Menu1_Load(object sender, EventArgs e)
{
master = new ToolStripMenuItem("Master");
menuStrip1.Items.Add(master);
master.DropDownItems.Add("Party Master");
master.DropDownItems.Add("Item Master");
master.DropDownItems.Add("Tax Master");
master.Click += MenuClicked;
transaction = new ToolStripMenuItem("Transaction");
menuStrip1.Items.Add(transaction);
transaction.DropDownItems.Add("Inward");
transaction.DropDownItems.Add("Inoice");
transaction.DropDownItems.Add("Daily Expense");
report = new ToolStripMenuItem("Report");
menuStrip1.Items.Add(report);
report.DropDownItems.Add("Master Report");
report.DropDownItems.Add("Transaction Report");
report.DropDownItems.Add("Daily Expense Report");
exit = new ToolStripMenuItem("Exit");
menuStrip1.Items.Add(exit);
}
private void MenuClicked(object o,EventArgs e)
{
if ((((ToolStripMenuItem)o).Text) == "Party Master")
{
Master.PartyMaster p = new Master.PartyMaster();
p.Show();
}
}`
`// Master
master.DropDownItems.
AddRange(new System.Windows.Forms.ToolStripItem[]
{
partyMaster,
itemMaster,
taxMaster
}
);
master.Name = "Master";
master.Size = new System.Drawing.Size(125, 20);
master.Text = "Master";
master.Click += new System.EventHandler(master_Click);
// Party Master
partyMaster.Name = "PartyMaster";
partyMaster.Size = new System.Drawing.Size(152, 22);
partyMaster.Text = "PartyMaster";
partyMaster.Click += new System.EventHandler(partyMaster_Click);
// Item Master
itemMaster.Name = "ItemMaster";
itemMaster.Size = new System.Drawing.Size(152, 22);
itemMaster.Text = "ItemMaster";
// Tax Master
taxMaster.Name = "TaxMaster";
taxMaster.Size = new System.Drawing.Size(152, 22);
taxMaster.Text = "TaxMaster"; //`
Or more dynamically with List and for loop.
List<ToolStripMenuItem> items = new List<ToolStripMenuItems>();
And loop to add more items to the Menu
ToolStripMenuItem item = new ToolStripMenuItem();
items.Add(item);
item.Click += new EventHandler(MenuClicked); // if you want to stick with only one function
Try add your dropdown items this way:
ToolStripItem partyMaster = new ToolStripMenuItem() { Text = "Party Master" };
partyMaster.Click += MenuClicked;
ToolStripItem itemMaster = new ToolStripMenuItem() { Text = "Item Master" };
itemMaster.Click += MenuClicked;
ToolStripItem taxMaster = new ToolStripMenuItem() { Text = "Tax Master" };
taxMaster.Click += MenuClicked;
master.DropDownItems.Add(partyMaster);
master.DropDownItems.Add(itemMaster);
master.DropDownItems.Add(taxMaster);

'System.ArgumentException' occurred in Xamarin.Forms.Platform.UAP.dll

Hi I create xamarin form project and implement MasterDetailPage it was working well without any issues in both android and ios, but i run into UWP project for windows 10 it throw the exception and app crashed. Please find my stacktrace
'System.ArgumentException' occurred in Xamarin.Forms.Platform.UAP.dll
at Windows.Foundation.Size..ctor(Double width, Double height)
at Xamarin.Forms.Platform.UWP.MasterDetailControl.get_DetailSize()
at Xamarin.Forms.Platform.UWP.MasterDetailPageRenderer.UpdateBounds()
at Xamarin.Forms.Platform.UWP.MasterDetailPageRenderer.OnIsPaneOpenChanged(DependencyObject sender, DependencyProperty dp)
at Windows.UI.Xaml.DependencyObject.SetValue(DependencyProperty dp, Object value)
at Xamarin.Forms.Platform.UWP.MasterDetailPageRenderer.UpdateIsPresented()
at Xamarin.Forms.Platform.UWP.MasterDetailPageRenderer.OnElementPropertyChanged(Object sender, PropertyChangedEventArgs e)
at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
at Xamarin.Forms.BindableObject.OnPropertyChanged(String propertyName)
at Xamarin.Forms.Element.OnPropertyChanged(String propertyName)
at Xamarin.Forms.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)
at Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
at Xamarin.Forms.MasterDetailPage.UpdateMasterBehavior(MasterDetailPage page)
My code
public class MasterHomePage : MasterDetailPage, MasterHomePageLisener
{
public MasterHomePage()
{
Title = AppRex.home;
Icon = "hamburger.png";
HomePageItem[] homePageItems =
{
new HomePageItem(AppRex.home, "home_icon.png", typeof(AfterLoginHomePage)),
new HomePageItem(AppRex.changePassword, "change_password_icon.png", typeof(ChangePassword)),
new HomePageItem("Set / Change PIN", "change_pin.png", typeof(LoginPinSetUpPage)),
new HomePageItem(AppRex.customerSuppoert, "support_icon.png", typeof(CustomerSupport)),
new HomePageItem(AppRex.about, "about_icon.png", typeof(AboutPage)),
new HomePageItem(AppRex.checkUpdate, "updates_icon.png", typeof(CheckUpdatedPage)),
new HomePageItem("Tutorial", "video_icon.png", typeof(TutorialVideoPage)),
new HomePageItem(AppRex.logout, "logout_icon.png", null),
};
this.Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
ListView list = new ListView
{
ItemsSource = homePageItems,
ItemTemplate = new DataTemplate(() =>
{
var imageCell = new LeftMenuCell();
imageCell.SetBinding(LeftMenuCell.TitleProperty, "Name");
imageCell.SetBinding(LeftMenuCell.ImageProperty, "IconSource");
return imageCell;
}),
VerticalOptions = LayoutOptions.FillAndExpand,
};
//Left menu
this.Master = new EgnatiumFull.Views.MyContentPage
{
Title = AppRex.home,
Content = new StackLayout { Children = { new Image() { Source = App.GetLogoImg()} ,
new MyLabel{Text = AppRex.version, HorizontalTextAlignment = TextAlignment.Center},
new MyLabel{Text = App.UserName, FontSize = 24, FontAttributes= FontAttributes.Bold},
new MyLabel{Text = App.UserEmailId},
new BoxView { BackgroundColor = Color.Gray, HeightRequest = 1}, list }
}
};
this.Detail = new AfterLoginHomePage(this);
if (Device.OS == TargetPlatform.WinPhone)
{
(this.Detail as EgnatiumFull.Views.MyContentPage).Content.GestureRecognizers.Add(
new TapGestureRecognizer((view) =>
{
this.IsPresented = true;
}));
}
// Define a selected handler for the ListView.
list.ItemTapped += async(sender, args) =>
{
var homePageItem = ((HomePageItem)args.Item);
var type = homePageItem.TargetType;
if (type == null)
{
if (homePageItem.Name.Equals(AppRex.logout))
{
list.SelectedItem = null;
var resp = await DisplayAlert("","Are you sure want to Logout?","Yes", "No");
if (resp)
{
await App.UpdateLoginStatus(true);
Application.Current.MainPage = App.GetPage();
}
}
}
else {
Page page = null;
if (type.Equals(typeof(AfterLoginHomePage)))
{
page = new AfterLoginHomePage(this);
}
else if (type.Equals(typeof(AboutPage)))
{
page = new AboutPage(this);
}
else if (type.Equals(typeof(ChangePassword)))
{
page = new ChangePassword(this);
}
else if (type.Equals(typeof(LoginPinSetUpPage)))
{
page = new LoginPinSetUpPage(App.UserId, App.UserEmailId, this);
}
else if (type.Equals(typeof(TutorialVideoPage)))
{
page = new TutorialVideoPage(this);
}
else if (type.Equals(typeof(CustomerSupport)))
{
page = new CustomerSupport(this);
}
else if (type.Equals(typeof(CheckUpdatedPage)))
{
page = new CheckUpdatedPage(this);
}
page.Title = ((HomePageItem)args.Item).Name;
Detail = page;
}
// Show the detail page.
this.IsPresented = false;
((ListView)sender).SelectedItem = null;
};
// Initialize the ListView selection.
//list.SelectedItem = homePageItems[0];
}
public void HomeIconClicked()
{
this.Detail = new AfterLoginHomePage(this);
}
public void MenuIconClicked()
{
this.IsPresented = !this.IsPresented;
}
}
public interface MasterHomePageLisener
{
void MenuIconClicked();
void HomeIconClicked();
}

Categories