Click in a children in a custom xamarin forms layout - xamarin

I have this class derived from a Layout<view> which is the WrapLayout example from Creating a Custom Layout:
Tthe code is a little big, so I put it in this pastebin:
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace CustomLayout.ViewModels
{
// ...
// [EDIT] puting the relevant code for put the images whit
/// a tap gesture in the layout
var images = GetImages();
int i = 0;
foreach (var photo in images.Photos)
{
var image = new Image
{
Source = ImageSource.FromResource(photo),
WidthRequest = 50,
HeightRequest = 75
};
var cartaoTap = new TapGestureRecognizer();
cartaoTap.Tapped += (s, e) =>
{
cartaoClick(i);
};
image.GestureRecognizers.Add(cartaoTap);
customLayout.Children.Add(image);
i++;
}
// ...
private void cartaoClick(int index){
Navigation.PushAsync(new CartaoPage(cartao.GetImagem(index), cartao.GetNome(index), cartao.GetDescricao(index)));
}
}
pastebin Raw Code for the CustomLayout class
"Cartao" is a class with attributes like Nome, Descricao and Imagem.
In the tap gesture event I pass the int "i" value to the CartaoClcik(int index) method when I added the image in the CustomLayout, in my mind, I can get the index passed to the call of this method when I added the image in the click of the mouse in one of the image showed, but, when I run and click in a image, the index parameter is alawys of the last image added
[EDIT:].
After an inhumane search job on the internet, I see that is possible to implement a class derived from Image() and then implement the GestureRecognizer command directly in this class, but I dont know how I can implement that and also I dont know how to pass the parameter needed for the page to show the image in full screen in the `CartaoPage(Image img, string nome, string descr) whose parameters have to be passed in when the user click in a image showed in they screen
someone can help me?

[SOLVED]
I solved this question, to solve I created a class derived from Image() with a public int ID {get; set;} and then fill this attribute when I instantiate my image using my own class and then I pass this ID for my index.
I know that not is a elegant solution but is working here.
thanks for all

Related

Changing size of Xamarin Flyout page

I'm trying to make a Flyout page (previously MasterDetailPage) take up a 1/3 of the screen for the Flyout and 2/3 for Detail.
I was able to accomplish this on iOS by using a custom renderer that's a modification of the Xamarin.Form's Flyout implementation
But there isn't any such implementation for Android and I can't figure out how to accomplish this.
Anyone know how to do this?
You could use custom renderer to do that.
The renderer of FlyoutPage in Android is FlyoutPageRenderer. The following link lists the renderer and native control classes that implement each Xamarin.Forms Page type:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/renderers
The source code of FlyoutPageRenderer:
https://github.com/xamarin/Xamarin.Forms/blob/5.0.0/Xamarin.Forms.Platform.Android/AppCompat/FlyoutPageRenderer.cs
You could get the field _flyoutLayout in source code. Then, you could set the height and width like the code below.
[assembly: ExportRenderer(typeof(FlyoutPage), typeof(FlyoutPageCustomRenderer))]
namespace App14.Droid
{
class FlyoutPageCustomRenderer: FlyoutPageRenderer
{
public FlyoutPageCustomRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(VisualElement oldElement, VisualElement newElement)
{
base.OnElementChanged(oldElement, newElement);
var fieldInfo = GetType().BaseType.GetField("_flyoutLayout", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var _flyoutLayou = (ViewGroup)fieldInfo.GetValue(this);
var lp = new LayoutParams(_flyoutLayou.LayoutParameters);
lp.Width = 400;
lp.Height = 600;
lp.Gravity = (int)GravityFlags.Left;
_flyoutLayou.LayoutParameters = lp;
}
}
}
For better effect, i set the background color to pink. The background color is set in flyout menu page.
If you wanna more information about the FlyoutPage, you could refer to the MS docs.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/flyoutpage

Xamarin Forms iOS TitleBarTextColor not changing

To change the TitleBarTextColor so far I tried a lot and my code now does change the back button color and the area on the very top of the screen, but the title!
in my AppDelegate FinishedLaunching function (it's after Forms.Init() and before LoadApplication()):
UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes
{
TextColor = UIColor.White
});
In my ViewModel:
(App.Current.MainPage as NavigationPage).BarBackgroundColor = Color.FromHex("#990000");
(App.Current.MainPage as NavigationPage).BarTextColor = Color.White;
And this is how I'm navigating between the pages (not all the pages):
await _navigationService.NavigateAsync(new Uri("http://wwww.x.com/NavigationPage/TabbedNavigationPage?selectedTab=XPage/Document", UriKind.Absolute));
I even try to call the ViewModel code after the Prism navigation but it doesn't work... I'm a beginner and don't fully comprehend Prism and or Xamarin Forms.
[EDIT] -> I also tried to create a new class and inherit it from NavigationPage, set the BarTextColor in its constructor and use that class in the navigation like this: await _navigationService.NavigateAsync(new Uri("http://wwww.x.com/NEWCLASSCREATED/TabbedNavigationPage?selectedTab=XPage/Document", UriKind.Absolute)); But, as you may know, it is still not working.
Here's a Image ;)
imageToSeeThatImNotLying
Thank you for your support!
So I finnaly managed to work this out...
What I had to do was to create a custom Content Page because all the other solutions wasn't working. So I created this custom renderer only in my iOS project:
[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomContentPageRenderer))]
namespace TestProject.iOS.Bll.Utils.Renderers
{
public class CustomContentPageRenderer : PageRenderer
{
public override void DidMoveToParentViewController(UIViewController parent)
{
base.WillMoveToParentViewController(parent);
var titleView = new UITextView();
var page = this.Element as ContentPage;
try
{
if (!string.IsNullOrEmpty(page.Title))
{
titleView.Text = page.Title;
titleView.TextColor = UIColor.White;
titleView.Font = UIFont.SystemFontOfSize(17, UIFontWeight.Regular);
var bgColor = page.BackgroundColor;
titleView.BackgroundColor = UIColor.FromRGBA((int)bgColor.R, (int)bgColor.G, (int)bgColor.B, 0);
parent.NavigationItem.TitleView = titleView;
parent.NavigationItem.TitleView.ContentMode = UIViewContentMode.ScaleAspectFit;
}
}
catch (Exception e)
{
}
}
}
}
And I also removed all the code that I puted before in my AppDelegate file and in the App.xaml.cs file as well. I left the codes from the ViewModels because it was changing the back button to white, and I deleted the new NagivationPage class that I created before.
I'm going to explain why I did some of the things that you saw there:
To change the Title I created a UITextView() and set it to my NavigationItem.TitleView of the parent page. I set titleView.Text = page.Title; because my original page already have a title, so I'm just reusing it. And the backgroundcolor I had to do all of that so the backgroundcolor property works just in the way that I wanted.
And this DidMoveToParentViewController function was just so it can do all that before NavigationAsync from Prism.

Telerik UI For WinForms RadMessageBox font size

Is there a way to increase the default font size of the RadMessageBox for the whole application without having to create a custom theme?
If not, how to just increase the font on the default theme in Visual Style Builder? I tried just increasing the label and button font sizes, but the style builder reset the whole form that hosts the message box to look very plain and cutting the text of the label (see attached screenshot).
Thank you.
It is possible to customize the font for all RadMessageBoxes within an application.
Implement a method ('SetTelerikControlStyles') which is called before any form and/or RadMessageBox is created. You only need to call this method once!
Add the following lines of code in the created method of step 1:
RadMessageBox.Instance.FormElement.TitleBar.Font = new Font("Calibri", 25f);
RadMessageBox.Instance.Controls["radLabel1"].Font = new Font("Calibri", 50f, FontStyle.Regular);
The FormElement.TitleBar.Font, as the name already reveals, is the title bar of the RadMessageBox.
Telerik is using dynamic named controls, so in this case radLabel1 represents the RadMessageBox text area.
Complete Program.cs sample:
using System;
using System.Drawing;
using System.Windows.Forms;
using Telerik.WinControls;
namespace WindowsFormsApplication
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SetTelerikControlStyles();
Application.Run(new Form1());
}
private static void SetTelerikControlStyles()
{
RadMessageBox.Instance.FormElement.TitleBar.Font = new Font("Calibri", 25f);
// I added this additional check for safety, if Telerik modifies the name of the control.
if (RadMessageBox.Instance.Controls.ContainsKey("radLabel1"))
{
RadMessageBox.Instance.Controls["radLabel1"].Font = new Font("Calibri", 50f, FontStyle.Regular);
}
}
}
}

Xamarin Forms Force view to bind values

I have a view. I have a bindable property there.
public partial class OrderCard : ContentView
{
public static readonly BindableProperty OrderProperty = BindableProperty.Create(nameof(Order), typeof(Order), typeof(OrderCard), null);
public Order Order
{
get { return (Order)GetValue(OrderProperty); }
set { SetValue(OrderProperty, value); }
}
public OrderCard()
{
InitializeComponent();
}
}
In the xaml of this view I'm binding to Order property like this:
Text="{Binding Order.Name, Source={x:Reference Root}}"
Root is a name in the xaml of a view OrderCard
When I use this view in the page everything works ok.
But I want to measure it's size even before adding it to the page.
var orderCard = new OrderCard { Order = order};
SizeRequest sizeRequest = orderCard.Measure(OrdersContainer.Width/5, OrdersContainer.Height);
But it gives me wrong numbers because bindings isn't applied.
How to force to apply bindings when view isn't attached to the page?
Bindings do not require being attached to a Page or anything else to be applied.
You might think they're not applied if your method for figuring out is to set a breakpoint on get_Order because that is never used, the Xaml loader uses GetValue directly. The usual way of figuring out if a Binding is correctly applied, is to add a PassthroughConverter (don't look for it, you have to write it yourself) to the Binding and put the breakpoint in the Convert method.
That being said, you can't Measure anything unless it's added to a page that is rendered on screen. If you try to Measure before that, you indeed get dummy values.
I was able to solve this problem by not doing a property Order but passing an order as BindingContext. Then I can measure the size of a view without attaching it to a page like this:
var orderCard = new OrderCard { BindingContext = order};
SizeRequest sizeRequest = orderCard.Measure(widthToTryToFitInTheView,heightToTryToFitInTheView);

Qt TableView - How to show image, when mouse hover?

I've got table view that is reading from SQL Database in one of my Qt project. It contains names column, and column that store the path of some image. How i can show the image like a ToolTip, when i do hover with the mouse on some of the image paths? I 've activated the mouse tracking, but i really don't know how to use it. If you can please provide some example. Than you a lot in advance.
The view's model should respond to Qt::TooltipRole and return the tooltip's content. It can contain HTML tags, including img tag that displays an image:
class Model : public QAbstractTableModel {
public:
//...
QVariant data(const QModelIndex &index, int role) const {
if (role == Qt::ToolTipRole) {
// find path for specified index
return QString("<img src='%1'>").arg(path);
}
//...
}
If you're using a built-in model class such as QSqlRelationalTableModel, you can create a subclass of that class and reimplement data function.

Resources