I'm having a few problems creating an integer attribute.
So, as background. I have got a custom canvas view. I have already successfully created a custom string attribute on that. The axml looks like this:
<turbineApp.droid.infrastructure.customViews.TurbineMapCanvas
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/VicMapCanvas"
custom:mapName="VIC"
/>
And the attribute comes from attrs.xml:
<resources>
<declare-styleable name="CustomCanvasView">
<attr name="mapName" format="string" />
</declare-styleable>
</resources>
I was hoping to add an integer attribute. So, I added the following as an element in attrs.xml:
<attr name="originY" format="integer" />
But now the project won't build. At all. With a fail message that the Resources class never got built.
I've unblocked myself by just making it a string and parsing the string in the code. But why will an integer not work? Have the good people at Xamarin not implemented that yet?
Thanks
Related
I am trying to use two converters together in my code. One is custom, the other one is the built-in converter.
My goal is to use my converter-modified value as a parameter in another MvvmCross converter.
So, I want to get System.Drawing.Color() first via DueDateTintValueConverter, then I will give it to MvvmCross' "NativeColor" converter.
And finally, the custom binder named TintColor will handle the setting of the Android coloring.
<ImageView
android:id="#+id/imageViewWatch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:tint="?attr/color_dueDate_normal"
app:MvxBind="TintColor NativeColor(DueDate), Converter=DueDateTint"
app:srcCompat="#drawable/ic_clock" />
But, I run into a binding error here getting "InvalidCastException, nullable DateTime cannot cast to Color".
It means DueDate remains the same regardless of the DueDateTintConverter.
How can I find a solution for this scenario?
Thanks!
Xamarin team introduced 2 different way of stand-alone-resource-dictionaries. Obviously there is a big difference but they neglected to write in their documentation.
Basically if i use without cs code behind file and without x:class defined as defined in the documentation and referencing like that in my page
<ResourceDictionary Source="GradientStyles.xaml"/>
GradientStyles.xaml look like below and i am updating GradientStartColor , GradientStopColor of PancakeView on runtime based on the theme selected.
<pancakeView:GradientStopCollection x:Key="BackgroundGradientStops">
<pancakeView:GradientStop Color="{StaticResource GradientStartColor}"
Offset="0"/>
<pancakeView:GradientStop Color="{StaticResource GradientStopColor}"
Offset="0.74"/>
</pancakeView:GradientStopCollection>
Implementation above just not working. It doesn't update Colors at all.
The same exact code;
Works in ContentPage.Resources directly. It will update
Works if i define a stand-alone-resource-dictionary with cs code behind file and implement on the page like this below
<ResourceDictionary.MergedDictionaries>
<resDics:GradientStyles />
</ResourceDictionary.MergedDictionaries>
There is that annotation in the documentation but i don't get what exactly it means.
This syntax doesnot instantiate the MyResourceDictionary class.
Instead, it references the XAML file.
Does it mean that it extends the contentpage using like partial class or inherits. Or is it cached only once and remains static? And when you use it with code behind class, it creates a new instance every time?
Finally is that a bug or feature? :)
I want to display an Image in my Xamarin forms project using XAML but is is not displaying.
This is the code:
<Image x:Name="myimage" Source="logo.jpg"/>
I have set myimage's build action to embedded resource. I also tried this
<Image x:Name="myimage" Source="MyXamarinProject.logo.jpg"/>
Where MyXamarinProject is my namespace. But both not working. What is wrong here?
The recommended way to avoid decreasing application performance or problems displaying the images on different screen resolutions is adding each image on each platform specific folder:
Android: Project -> Resources -> Drawable (or each resolution drawable folder (-hdpi,-xhdpi...))
iOS: You can create an asset catalog for each image or use Project -> Resources folder.
And then, use the image name on XAML file. No code needed.
Upon inspecting the Microsoft Docs for this, it is not as easy as just specifying the path.
First, Create an IMarkupExtension
[ContentProperty (nameof(Source))]
public class ImageResourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue (IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly);
return imageSource;
}
}
Then consume it in your XAML:
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages"
x:Class="WorkingWithImages.EmbeddedImagesXaml">
<StackLayout VerticalOptions="Center" HorizontalOptions="Center">
<!-- use a custom Markup Extension -->
<Image Source="{local:ImageResource WorkingWithImages.beach.jpg}" />
</StackLayout>
</ContentPage>
Note that you have to add a custom namespace to your page, in this case: xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages"
So, I can not display an image using XAML only. I am pretty satisfied with both the answers. Although this is how I displayed the image without pasting into the android and iOS(to their respective folders) and without making IMarkupExtension.
I put one line of code into C# code behind and that worked:
myimage.Source = ImageSource.FromResource("MyXamarinProject.logo.jpg");
//logo.jpg must be Embedded Resource.
This could not be the right answer but it can be a choice. Thanks!
I had the same problem on Visual Studio Mac:
my XAML :
<Image x:Name="myimage" Source="logo.jpg"/>
my resources was : logo.jpg
But the file was saved in the file system as Logo.jpg (Capital L).
I just modified it to logo.jpg and rebuild all.
is there any way to bind in layout directly to custom fragment?
ie:
<fragment android:id="#+id/AwesomeFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="CustomTarget PropertyInVM;" class="My.Awesome.Fragment" />
CustomTarget is CustomTargetBinding
So how you can do this:
create custom control with properties you need
create target bindings
place custom control into layout instead of your fragment
bind from layout
I've been trying to build a Tree Table and define the cellValueFactories in FXML.
When I try this, I get the following error in the stack trace
Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: javafx.scene.control.TreeTableColumn$CellDataFeatures cannot be cast to javafx.scene.control.TableColumn$CellDataFeatures
The FXML is written as follows
<TreeTableView prefHeight="500.0" prefWidth="600.0" fx:id="customerContractsTable">
<columns>
<TreeTableColumn prefWidth="116.0" text="Contract Number">
<cellValueFactory>
<PropertyValueFactory property=""/>
</cellValueFactory>
</TreeTableColumn>
</columns>
</TreeTableView>
I've been trying to find any documentation references on how to do this and am coming up completely empty handed. Any documentation or help would be greatly apperciated.
Here is a sample TreeTableView in FXML.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TreeTableView?>
<?import javafx.scene.control.TreeTableColumn?>
<?import javafx.scene.control.cell.TreeItemPropertyValueFactory?>
<TreeTableView>
<columnResizePolicy>
<TreeTableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
<columns>
<TreeTableColumn text="Contract Number">
<cellValueFactory>
<TreeItemPropertyValueFactory property="contractNumber" />
</cellValueFactory>
</TreeTableColumn>
</columns>
</TreeTableView>
See RT-35233.
I think it's a bug, you should log it in the JavaFX issue tracker.
I think the bug is that the TreeTableView does not define it's own builder for defining columns in FXML, so it inherits the functionality from a TableViewBuilder which only defines columns as TableColumns, not TreeTableColumns. This means that TableColumns for straight TableViews can be defined in FXML but, TableColumns for TreeTableViews cannot.
In the meantime, define your cellValueFactories for TreeTables in code (in the initializer for your controller) rather than FXML.
I had the same problem but then checked and changed the imports.
I was building a Dynamic Tableview and had used the "Fix Imports" feature of Netbeans, however I noticed that instead of importing:
javafx.scene.control.TableColumn.CellDataFeatures;
it had imported
javafx.scene.control.TreeTableColumn.CellDataFeatures;
I corrected the import manually and the problem disappeared.