adding DateTime.Now into Xaml file in xamarion.forms - xaml

I'm trying to add DateTime.Now in xamarin.forms in xaml Page
i wrote this code :
<DatePicker Grid.Column="0" Grid.Row="0" FontSize="Small" Date=DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss") Format="d"></DatePicker>
But it's gave an error in DateTime.Now.ToString("dd'/'MM'/'yyyy HH:mm:ss")
section, it say :
Invalid value for Priority 'Date' : 'DateTime.Now.ToString'
and many other errors appear, how can i solve it ?

xaml:
<DatePicker Date="{x:Static system:DateTime.Today}" />
Add please:
xmlns:system="clr-namespace:System;assembly=mscorlib"

Related

TargetNullValue not working in ListView in Xamarin app

I'm trying to display some text -- or an image -- if there's no data for my ListView to display in my Xamarin Forms 5 app but I can't get it to work.
Here's what my ListView looks like:
<ListView
x:Name="CustomersList"
ItemsSource="{Binding Customers, TargetNullValue='No customers!'}"
BackgroundColor="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell
Text="{Binding FullName}"
TextColor="{StaticResource PrimaryDark}"
Command="{Binding Source={x:Reference Name=CustomersList}, Path=BindingContext.CustomerSelectedCommand}"
CommandParameter="{Binding .}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The source for the ListView is set as follows in my ViewModel
ObservableRangeCollection<CustomerModel> customers;
public ObservableRangeCollection<CustomerModel> Customers
{
get => customers;
set
{
if (customers == value)
return;
customers = value;
OnPropertyChanged();
}
}
If my API call returns nothing, I do a Customers = null;
When I use TargetNullValue as shown above, I acutally get errors where I bind the TextCell to FullName. It shows:
'FullName' property not found on 'N', target property:
'Xamarin.Forms.TextCell.Text'
It then repeats this error message for every character in No Customers!.
I then tried using FallbackValue. This time I don't get an error but it simply doesn't show the "No Customers!" message.
How do I handle a ListView having no data to show? At the very least, I'd like to show some simple text message but it would be even better to show something nicer, maybe an image.
If you are ok to switch from ListView to CollectionView, you can use the built-in CollectionView.EmptyView:
<CollectionView ItemsSource="{Binding Customers}"
EmptyView="No customers!" ... />
With the same EmptyView you can define whatever view:
<CollectionView>
<CollectionView.EmptyView>
<ContentView>
<image ... />
</ContentView>
</CollectionView.EmptyView>
</CollectionView>
Docs: CollectionView EmptyView

How to give name to BoxView in xaml?

I am working on xamarin.forms app and Instead of entry I want to use Boxview and Iam trying to name the Boxview and the following error is displayed
here is the code
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<!--<Entry x:Name="csv_file" Placeholder="" WidthRequest="150">
</Entry>-->
<BoxView x:Name="csv_file" Color="WhiteSmoke" WidthRequest="150"
HeightRequest="40" VerticalOptions="Center" HorizontalOptions="Center"
/>
<Button BackgroundColor="Gray" TextColor="Black"
WidthRequest="60" Text="...." Clicked="OnBrowse"/>
</StackLayout>
The error is
Severity Code Description Project File Line Suppression State
Error CS1061 'BoxView' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'BoxView' could be found (are you missing a using directive or an assembly reference?)
Can you suggest me if I have to add any assembly references ?
Thanks
You cannot use BoxView instead of an Entry. BoxView doesn't have Text property.
Xamarin.Forms Documentation:
BoxView renders a simple rectangle of a specified width, height, and
color. You can use BoxView for decoration, rudimentary graphics, and
for interaction with the user through touch.
Check the code-behind for the view. From the commented out code, it looks like you used to have an <Entry> with the same name, and you probably have code that attempts to set csv_file.Text, which might have worked for Entry, but won't for BoxView of course.

Cannot bind to button events using x:Bind in DataTemplates - generates XAML error

With the {x:Bind} markup syntax you can bind to events provided the method meets the following requirements:
Match the signature of the event.
OR have no parameters.
OR have the same number of parameters of types that are assignable from the types of the event parameters.
This works perfectly fine outside of a DataTemplate. Once the binding happens inside the DataTemplate the compiler generates the following error:
Xaml Internal Error error WMC9999: Object reference not set to an
instance of an object.
What is the fix for binding to events inside DataTemplates?
Full example code here.
Snippet of the example code below - note the first button (line 2) is fine and the second button (line 6) is also fine. If you comment out line 6 and and comment in line 7, the error occurs.
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Tapped="{x:Bind Click}" Content="WORKING"/>
<ListView ItemsSource="{x:Bind Names}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Customer">
<Button Content="{x:Bind Title}"/>
<!--<Button Tapped="{x:Bind Clicky}" Content="{x:Bind Title}"/>-->
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
I was able to get it to work with the following code:
<DataTemplate x:DataType="local:Customer">
<StackPanel Orientation="Vertical">
<Button Tapped="{x:Bind Clicky}" Content="{x:Bind Title}" />
</StackPanel>
</DataTemplate>
It seems as though you need to have it inside a container for it work. I have no idea why I am guessing magic.
The parser cannot find Clicky from the datacontext of the button while in the template. Because the object that is being handed to the button in the template (from the Names on the ItemSource of the parent) is not the same as outside the template which has a Clicky. You will need to bind Clicky to the page's datacontext to get it to work.
Otherwise turn off any design time operations by setting Tapped="{x:Bind Clicky, IsDesignTimeCreatable=False}.

XamlParseException while trying to bind Date of Windows 10 CalendarDatePicker

I am trying to bind the Date attribute of the CalendarDatePicker with a DateTime property in a Windows 10 Universal App:
<CalendarDatePicker
Date="{Binding ClientDetailsViewModel.BirthDate,
Converter={StaticResource DateTimeToDateTimeOffsetConverter} }"
Foreground="{StaticResource TextGreyBrush}" />
I am getting the following exception while the view initializes:
An exception of type 'Windows.UI.Xaml.Markup.XamlParseException'
occurred in KLMMobile.UI.exe but was not handled in user code
WinRT information: Failed to assign to property
'Windows.UI.Xaml.Controls.CalendarDatePicker.Date'. [Line: 93
Position: 41]
I've tested it with the DatePicker and it worked perfectly.
After lots of effort trying to bind the Date, I overcame this problem by using a trigger for DateChanged and binding the PlaceHolderText with the Property that should be displayed at start.
<CalendarDatePicker
PlaceholderText="{Binding UpdateWorkingTimeModel.StartTime, Converter={StaticResource TicksToDateTimeConverter}, ConverterParameter='dd.MM.yyyy', Mode=TwoWay}">
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="DateChanged">
<interactivity:InvokeCommandAction Command="{Binding DatePickedCommand}"/>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
</CalendarDatePicker>

Why is this XAML failing with "The property "Content" can only be set once."?

I've got this XAML:
<AppBarButton Icon="Protected" Label="Privacy Policy" >
<AppBarButton.Flyout>
<StackPanel>
<Flyout>
<TextBlock Text="Photrax extracts information from images you load into it. The information it extracts includes location information (where the photos were taken, when that is available) and the date and time the photo was taken. This data is stored in a local/internal/embedded (SQLite) database. This data is not stored in the cloud but only on your local device." TextWrapping="Wrap" FontSize="26" FontFamily="Verdana">
</TextBlock>
<TextBlock Text="To reiterate: Your data is not shared with anyone else. It is stored only on the device from which you use Photrax." TextWrapping="Wrap" FontSize="26" FontFamily="Verdana">
</TextBlock>
</Flyout>
</StackPanel>
</AppBarButton.Flyout>
</AppBarButton>
...which fails with, "The property "Content" can only be set once."
Why is there a problem with this, when the following XAML, which is basically the same, compiles fine:
<AppBarButton Icon="MapPin" Label="Colors In Use" Tapped="appbarbtnPhotosetColorMapping_Tapped">
<AppBarButton.Flyout>
<Flyout>
<StackPanel Background="Azure">
<TextBlock Text="Photoset:Pushpin Color Legend" TextWrapping="Wrap" FontSize="26" FontFamily="Verdana"></TextBlock>
<TextBlock x:Name="textblock0" Text="Unused" Foreground="Red" FontFamily="Segoe UI" FontSize="13" Margin="4" />
. . .
?
I get other err msgs with that xaml, too (about the following types being expected: flyoutbase and uielement), but I think it's the Content business that is giving me the business.
I pasted the problem code into kaxaml, but it doesn't even know what an AppBarButton is, and complained about that being an invalid element.
UPDATE
I can't test it yet, but I'm thinking that what Abdallah means is that I need to change this:
<StackPanel>
<Flyout>
. . .
</Flyout>
</StackPanel>
...to this:
<Flyout>
<StackPanel>
. . .
</StackPanel>
</Flyout>
You have two problems here :
1) The property "Content" can only be set once. :
You can't set the content for <Flyout> </Flyout> more than once and you set the content twice (Two TextBlocks) in first XAML code and in the working XAML once(One StackPanel), According to MSDN :
<Flyout>
singleUIElement
</Flyout>
singleUIElement :
A single object element that declares the content. This must be an
object that has UIElement in its hierarchy (plain strings don't work).
This can be a container, such as a Panel derived class, so that
multiple content items within the Flyout can be arranged in layout.
2) The following type was expected: "FlyoutBase".
You can't set Flyout property to any class that not derived from FlyoutBase class (i.e only Flyout and MenuFlyout ). you set the Flyout property in first XAML code to StackPanel and in the working XAML to Flyout .