I have a page in which I display two different charts, depending on a button click. At the moment I change charts this way:
protected void ClickedChangeChart()
{
if (chartType == true)
{
chartType = false;
Navigation.PushAsync (new MainPage (false));
}
else
{
chartType = true;
Navigation.PushAsync (new MainPage (true));
}
}
chartType is a bool and depending on it's value I choose which chart to load using this statement in OnAppearing():
protected override async void OnAppearing ()
{
// some code
ChartView chartView = new ChartView
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 300,
WidthRequest = 400
};
if (chartType == true)//true = candle
{
candleModel = new CandleModel ();
chartView.Model = await candleModel.GetModel ();
}
else if(chartType == false) //false = line
{
lineModel = new LineModel ();
chartView.Model = await lineModel.GetModel ();
}
//here I create a grid, add some children to it and the add the chartView
grid.Children.Add (chartView, 1, 6, 1, 3);
Content = grid;
}
The problem is that when I want to switch the charts I have to reload the whole page, which isn't what I want. How can I make it so when I click a button I calls a function which switches the model for the chart? I suppose it will be something like this, but I can't get it to work:
public async void ClickedButton()
{
grid.Children.Remove(chartView);
if (chartType == true)
{
candleModel = new CandleModel ();
chartView.Model = await candleModel.GetModel ();
}
else if(chartType == false)
{
lineModel = new LineModel ();
chartView.Model = await lineModel.GetModel ();
}
grid.Children.Add(chartView);
}
UPDATE:
Using Daniel Luberda's solution, I managed to get it to work with this:
btnChartType.Clicked += async delegate {
Device.BeginInvokeOnMainThread (async () => {
grid.Children.Remove (chartView);
if (isCandle == true) {
candleModel = new CandleModel ();
chartView.Model = await candleModel.GetModel ();
isCandle = false;
} else if (isCandle == false) {
lineModel = new LineModel ();
chartView.Model = await lineModel.GetModel ();
isCandle = true;
}
grid.Children.Add (chartView, 1, 6, 1, 3);
});
};
You can't get it to work because you have an async method which means that you're doing it on another thread. You can change UI only from UI thread. Also async void will swallow all exceptions, it's better to use async Task instead.
// THAT WILL WORK
public async void ClickedButton() // but public async Task would be better
{
Device.BeginInvokeOnMainThread(async () => {
grid.Children.Remove(chartView);
if (chartType == true)
{
candleModel = new CandleModel ();
chartView.Model = await candleModel.GetModel ();
}
else if(chartType == false)
{
lineModel = new LineModel ();
chartView.Model = await lineModel.GetModel ();
}
grid.Children.Add(chartView);
});
}
Related
I have this code:
public ScrollHeadingView2()
{
outerGrid = new Grid() { BackgroundColor = Color.Red, RowSpacing = 0, ColumnSpacing = 0 };
outerGrid.AddChild(new BoxView()
{
HeightRequest = 100,
WidthRequest = 100,
BackgroundColor = Color.Pink
}, 0, 0);
var tap1 = new TapGestureRecognizer()
{
NumberOfTapsRequired = 1
}.Bind(TapGestureRecognizer.CommandProperty, nameof(BackArrowTapped));
grid1.GestureRecognizers.Add(tap1);
}
private async Task BackArrowTapped()
{
await Shell.Current.Navigation.PopAsync(false);
}
The pink area of the grid displays but when I place a break on the await then it doesn't go there when I tap that area.
Does anyone have any idea what might be wrong?
BackArrowTapped is not a command. If you want to use an event handler, just set the gesture's Tapped property
tap1.Tapped += async (s,o) => { await BackArrowTapped(); };
This code fully demonstrates what looks like a bug. Sequence is...
App opens
Default main page created.
Login box popped up.
Login box closed. (Click on page to simulate)
New main page created.
Login box popped up.(Click on page to simulate)
Exception... Cannot access disposed object.
Any ideas?
using System;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace test333
{
public class Main1 : ContentPage
{
public Main1()
{
var btn = new Button();
btn.Clicked += (sender, e) =>
{
MessagingCenter.Send<object>(this, "LoginLogout");
Navigation.PopModalAsync();
};
Content = btn;
}
}
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new Main1 { BackgroundColor = Color.Red };
MessagingCenter.Subscribe<object>(this, "LoginLogout", s =>
Device.BeginInvokeOnMainThread(async () => await HandleLogOut()));
HandleLogOut();
}
bool login = true;
async Task HandleLogOut()
{
if (login)
{
await MainPage.Navigation.PushModalAsync(new Main1 { BackgroundColor = Color.Orange });
}
else
{
MainPage = new Main1 { BackgroundColor = Color.Green };
}
login = !login;
}
}
}
I think the problem happens in step 6:Login box popped up.(Click on page to simulate).
Have a look at below codes in btn.clicked:
btn.Clicked += (sender, e) =>
{
MessagingCenter.Send<object>(this, "LoginLogout");
Console.WriteLine("popAction");
Navigation.PopModalAsync();
};
And handle logout:
async Task HandleLogOut()
{
if (login)
{
await MainPage.Navigation.PushModalAsync(new Main1 { BackgroundColor = Color.Orange });
Console.WriteLine("PushModalAction");
}
else
{
MainPage = new Main1 { BackgroundColor = Color.Green };
}
login = !login;
}
I print the popAction and PushAction in your code, you will find that the popAction is executed before PushAction.
Solution:
You should perform popAction after pushModalAction is completed.
I moved the Navigation.PopModalAsync(); from the btn.clicked to HandleLogOut and then it works well.
btn.Clicked += async (sender, e) =>
{
MessagingCenter.Send<object>(this, "LoginLogout");
};
In handle logout:
async Task HandleLogOut()
{
if (login)
{
Console.WriteLine("PushModalAction");
await MainPage.Navigation.PushModalAsync(new Main1 { BackgroundColor = Color.Orange });
Console.WriteLine("popAction");
MainPage.Navigation.PopModalAsync();
}
else
{
MainPage = new Main1 { BackgroundColor = Color.Green };
}
login = !login;
}
I am using below code to scan bar codes. I have set the cancel and flash buttons texts but its not visible on the UI. Its working fine in iOS but not in Android.
See below code
public async Task StartScan()
{
var scanPage = new ZXing.Mobile.MobileBarcodeScanner();
this.cancelTimer = new CancellationTokenSource();
scanPage.AutoFocus();
scanPage.TopText = "Place the barcode between the red line";
scanPage.CameraUnsupportedMessage = "Camera doesnt supports AUTOFOCUS.";
scanPage.BottomText = "If it doesnt autofocus, touch the screen to autofocus";
scanPage.CancelButtonText = "<< Back";
scanPage.FlashButtonText = "Turn Flash";
scanPage.UseCustomOverlay = false;
Device.StartTimer(new TimeSpan(0, 0, 0, 3, 0), () =>
{
scanPage.AutoFocus();
// scanPage.BottomText = "focusing";
return true;
});
ZXing.Result result = null;
CancellationTokenSource cts = this.cancelTimer;
TimeSpan ts = new TimeSpan(0, 0, 0, 2, 0);
Device.StartTimer(ts, () =>
{
if (cts.IsCancellationRequested)
{
return false;
}
if (result == null)
{
scanPage.AutoFocus();
return true;
}
return false;
});
result = await scanPage.Scan(new MobileBarcodeScanningOptions
{
TryHarder = false,
AutoRotate = false,
UseNativeScanning = true,
PossibleFormats = GetAvailableFormats(),
});
if (result != null && !string.IsNullOrWhiteSpace(result.Text))
{
await Stop();
await ProcessResult(result.Text);
}
await Stop();
}
So from the above code, Bottom and Top texts are appearing but not the buttons for which code is like below.
scanPage.CancelButtonText = "<< Back";
scanPage.FlashButtonText = "Turn Flash";
Can anyone help me to get this?
I have a List-View in the content page, List view Items are picked from the SQLite. I want to refresh the page periodically so that I can able to show the latest items inserted in the sql lite.
1. When the first time I added record status of that record is "queued"in local db, List Item will be displayed and status of that Item will be shown as "[EmployeeNo] it is queued After 5 minutes it will be synced".
2.After 5 minutes,All local db [Sqlite] will be synced with the actual sql server, Then status of that record will be updated to "completed" in local db,Then status I want to show "[EmployeeNo] it is completed" in list view automatically.
Use an ObservableCollection<T> as your ItemSource - it will automatically update the UI whenever items are added or removed from it
StartTimer
Device.StartTimer (new TimeSpan (0, 0, 10), () => {
// do something every 10 seconds
return true; // runs again, or false to stop
});
public class EmployeeListPage : ContentPage
{
ListView listView;
public EmployeeListPage()
{
Title = "Todo";
StartTimer();
var toolbarItem = new ToolbarItem
{
Text = "+",
Icon = Device.OnPlatform(null, "plus.png", "plus.png")
};
toolbarItem.Clicked += async (sender, e) =>
{
await Navigation.PushAsync(new EmployeeItemPage
{
BindingContext = new Employee()
});
};
ToolbarItems.Add(toolbarItem);
listView = new ListView
{
Margin = new Thickness(20),
ItemTemplate = new DataTemplate(() =>
{
var label = new Label
{
VerticalTextAlignment = TextAlignment.Center,
HorizontalOptions = LayoutOptions.StartAndExpand
};
label.SetBinding(Label.TextProperty, "EmployeeName");
var labelText = new Label
{
VerticalTextAlignment = TextAlignment.Center,
HorizontalOptions = LayoutOptions.StartAndExpand
};
label.SetBinding(Label.TextProperty, "EmpStatusDisplayText");
var tick = new Image
{
Source = ImageSource.FromFile("check.png"),
HorizontalOptions = LayoutOptions.End
};
tick.SetBinding(VisualElement.IsVisibleProperty, "IsActive");
var stackLayout = new StackLayout
{
Margin = new Thickness(20, 0, 0, 0),
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children = { label, tick }
};
return new ViewCell { View = stackLayout };
})
};
listView.ItemSelected += async (sender, e) =>
{
EmployeeDatabindingDto dto = (e.SelectedItem as EmployeeDatabindingDto);
Employee emp = new Employee {EmployeeID=dto.EmployeeID,EmployeeName=dto.EmployeeName,Salary=dto.Salary,IsActive=dto.IsActive };
Debug.WriteLine("Employee ResumeAt Id = " + emp.EmployeeID);
await Navigation.PushAsync(new EmployeeItemPage
{
BindingContext = emp
});
};
Content = listView;
}
protected override async void OnAppearing()
{
base.OnAppearing();
List<Employee> employees = await App.EmpDatabase.GetItemsAsync();
List<EmployeeDatabindingDto> listBindingDto = await MapEmpWithEmpBindingDto(employees);
listView.ItemsSource = listBindingDto;
}
public async Task<List<EmployeeDatabindingDto>> MapEmpWithEmpBindingDto(List<Employee> employees)
{
List<EmployeeDatabindingDto> bindEmployees = new List<EmployeeDatabindingDto>();
foreach (var employee in employees)
{
string displaysText = "";
string displayDate = "";
displayDate = employee.IsActive == false ? employee.Createddate.ToString() : employee.Modifieddate.ToString();
displaysText = employee.IsActive == false ? string.Format("{0} {1}", "is in queued on", displayDate) : string.Format("{0} {1} ", "is submitted on", displayDate);
bindEmployees.Add(new EmployeeDatabindingDto
{
EmployeeID = employee.EmployeeID
,
EmployeeName = employee.EmployeeName
,
Salary = employee.Salary
,
Createddate = employee.Createddate
,
IsActive = employee.IsActive
,
EmpStatusDisplayText = string.Format("{0} {1}", employee.EmployeeName, displaysText)
});
}
return bindEmployees;
}
private void StartTimer()
{
Device.StartTimer(System.TimeSpan.FromSeconds(10), () =>
{
List<Employee> employees = App.EmpDatabase.GetItemsAsync().Result;
List<EmployeeDatabindingDto> listBindingDto = MapEmpWithEmpBindingDto(employees).Result;
listView.ItemsSource = listBindingDto;
Device.BeginInvokeOnMainThread(UpdateUserDataAsync);
return true;
});
}
private async void UpdateUserDataAsync()
{
await App.EmpDatabase.UpdateEmpStatusAsync();
}
}
I have an Image view I'm having trouble setting the source for. I'm using a button to execute a TakePictureCommand which calls the TakePicture() method (shown below) which in turn sets my source "ImageSource". Debugging the method shows the image is coming in, but I never see it come up in the UI.
I may not be setting the binding for the Image properly, this is what I have:
Image avatar = new Image();
avatar.Source = ImageSource;
Button setImageBtn = new Button{ Text = "Photo" };
setImageBtn.Clicked += async (sender, e) =>
{
string action = await DisplayActionSheet(
"Event Photo", "Cancel", null, OPTION_CAMERA, OPTION_GALLERY);
if(action == OPTION_CAMERA) {
TakePictureCommand.Execute(null);
}
else if(action == OPTION_GALLERY) {
SelectPictureCommand.Execute(null);
}
};
TakePicture()
private async Task<MediaFile> TakePicture()
{
Setup();
ImageSource = null;
return await _mediaPicker.TakePhotoAsync(
new CameraMediaStorageOptions {
DefaultCamera = CameraDevice.Front,
MaxPixelDimension = 400
}).ContinueWith(t =>
{
if (t.IsFaulted)
{
Status = t.Exception.InnerException.ToString();
}
else if (t.IsCanceled)
{
Status = "Canceled";
}
else
{
var mediaFile = t.Result;
ImageSource = ImageSource.FromStream(() => mediaFile.Source);
return mediaFile;
}
return null;
}, _scheduler);
}
What am I missing here?
Below approach is working for me:
First, in XAML I added Image view
<Image Source="{Binding ImageSource}" VerticalOptions="Fill" HorizontalOptions="Fill"
Aspect="AspectFit"/>
Second, in ViewModel I added ImageSource property like this:
public ImageSource ImageSource
{
get { return _imageSource; }
set { this.SetProperty(ref _imageSource, value); }
}
Third, in command handler:
await TakePicture ();
Forth, code of TakePicture() method the same as you wrote.
My Setup():
if (_mediaPicker != null)
return;
var device = Resolver.Resolve<IDevice>();
_mediaPicker = DependencyService.Get<IMediaPicker>() ?? device.MediaPicker;