|
1 | 1 | # SharedXFormCoreLibrary |
2 | 2 | A shared library containing recurring features & utilities for .NETStandard Xamarin.Forms applications |
| 3 | + |
| 4 | +# Nuget |
| 5 | +Get the latest version from nuget.org<br> |
| 6 | +[](https://www.nuget.org/packages/SharedXFormCoreLibrary/) |
| 7 | +[](https://www.nuget.org/packages/SharedXFormCoreLibrary) |
| 8 | + |
| 9 | +## Available content |
| 10 | +Please find a list of available content below. |
| 11 | + |
| 12 | +### Behaviors |
| 13 | + |
| 14 | +#### Namespace |
| 15 | +```xaml |
| 16 | +xmlns:behaviors="clr-namespace:AndreasReitberger.Shared.XForm.Core.Behaviors;assembly=SharedXFormCoreLibrary" |
| 17 | +``` |
| 18 | + |
| 19 | +#### EventToCommandBehavior |
| 20 | +This `Behavior` executes a `Command` when the specified `Event` is fired. |
| 21 | + |
| 22 | +```xaml |
| 23 | +<sliders:SfSlider |
| 24 | + Margin="0,4" |
| 25 | + Minimum="0" Maximum="{Binding LimitFan}" |
| 26 | + StepSize="1" |
| 27 | + MinorTicksPerInterval="100" |
| 28 | + ShowLabels="True" |
| 29 | + > |
| 30 | + <sliders:SfSlider.Behaviors> |
| 31 | + <behaviors:EventToCommandBehavior |
| 32 | + EventName="ValueChangeEnd" |
| 33 | + Command="{Binding SetFanCommand}" |
| 34 | + /> |
| 35 | + </sliders:SfSlider.Behaviors> |
| 36 | +</sliders:SfSlider> |
| 37 | +``` |
| 38 | + |
| 39 | +### Converters |
| 40 | + |
| 41 | +#### Namespace |
| 42 | +```xaml |
| 43 | +xmlns:converters="clr-namespace:AndreasReitberger.Shared.XForm.Converters;assembly=SharedXFormCoreLibrary" |
| 44 | +``` |
| 45 | + |
| 46 | +#### BooleanReverseVisibilityConverter & BooleanVisibilityConverter |
| 47 | +This `Converter` converts a `Boolean` into a reverse and non-reverse visibility (if `value` is `true`, it returns `false`) |
| 48 | + |
| 49 | +```xaml |
| 50 | +<Label |
| 51 | + Style="{StaticResource SmallLabelStyle}" |
| 52 | + TextColor="{DynamicResource Error}" |
| 53 | + Text="{x:Static localization:Strings.NotAvailableDots}" |
| 54 | + IsVisible="{Binding HasDoubleExtruder, Converter={StaticResource BooleanReverseVisibilityConverter}}" |
| 55 | + /> |
| 56 | +``` |
| 57 | + |
| 58 | +#### ByteArrayToImageConverter |
| 59 | +This `Converter` converts a `byte[]` into an `Image` |
| 60 | + |
| 61 | +```xaml |
| 62 | +<Image |
| 63 | + VerticalOptions="Start" |
| 64 | + Margin="{OnIdiom Phone='-4,0', Tablet='-62,0', Default='-4,0'}" |
| 65 | + Source="{Binding Thumbnail, Converter={StaticResource ByteArrayToImageConverter}}" |
| 66 | + > |
| 67 | + <Image.Style> |
| 68 | + <Style TargetType="Image"> |
| 69 | + <Setter Property="Aspect" Value="AspectFit"/> |
| 70 | + <Style.Triggers> |
| 71 | + <DataTrigger |
| 72 | + TargetType="Image" |
| 73 | + Binding="{Binding IsPortrait}" |
| 74 | + Value="False"> |
| 75 | + <Setter Property="Aspect" Value="AspectFill"/> |
| 76 | + </DataTrigger> |
| 77 | + </Style.Triggers> |
| 78 | + </Style> |
| 79 | + </Image.Style> |
| 80 | +</Image> |
| 81 | +``` |
| 82 | + |
| 83 | +#### ColorToBlackWhiteConverter |
| 84 | +This `Converter` converts a `Color` into `Colors.White` or `Colors.Black`. This helps to get the |
| 85 | +accurate `oreground` for a colored `Background`. |
| 86 | + |
| 87 | +```xaml |
| 88 | +<Border |
| 89 | + BackgroundColor="{Binding HexCode, Converter={StaticResource StringToColorConverter}, Mode=OneWay}" |
| 90 | + > |
| 91 | + <Label |
| 92 | + Text="{Binding HexCode}" |
| 93 | + TextColor="{Binding Source={RelativeSource AncestorType={x:Type Border}}, Path=BackgroundColor, Converter={StaticResource ColorToBlackWhiteConverter}, Mode=OneWay}" |
| 94 | + Style="{StaticResource LabelStyle}" |
| 95 | + VerticalTextAlignment="Center" |
| 96 | + HorizontalTextAlignment="Center" |
| 97 | + /> |
| 98 | + </Border> |
| 99 | +``` |
| 100 | + |
| 101 | + |
| 102 | +#### DoubleHoursToTimeSpanConverter |
| 103 | +This `Converter` converts a `Double` into a `TimeSpan`. |
| 104 | + |
| 105 | +```xaml |
| 106 | +<Label |
| 107 | + LineBreakMode="WordWrap" Margin="2,10,0,10" |
| 108 | + VerticalTextAlignment="Center" |
| 109 | + FontSize="{OnIdiom Tablet=14, Default=12}" |
| 110 | + > |
| 111 | + <Label.Style> |
| 112 | + <Style TargetType="Label" BasedOn="{StaticResource TitleViewHeadlineLabelStyle}"> |
| 113 | + <Setter Property="IsVisible" Value="False"/> |
| 114 | + <Style.Triggers> |
| 115 | + <MultiTrigger |
| 116 | + TargetType="Label"> |
| 117 | + <MultiTrigger.Conditions> |
| 118 | + <BindingCondition Binding="{Binding IsPrinting, Mode=TwoWay}" Value="True"/> |
| 119 | + <BindingCondition Binding="{Binding ShowRemainingPrintTimeInTitleView}" Value="True"/> |
| 120 | + </MultiTrigger.Conditions> |
| 121 | + <Setter Property="IsVisible" Value="True"/> |
| 122 | + </MultiTrigger> |
| 123 | + </Style.Triggers> |
| 124 | + </Style> |
| 125 | + </Label.Style> |
| 126 | + <Label.FormattedText> |
| 127 | + <FormattedString> |
| 128 | + <Span Text="("/> |
| 129 | + <Span Text="{Binding RemainingPrintTime, Converter={StaticResource DoubleHoursToTimeSpanConverter}}"/> |
| 130 | + <Span Text=")"/> |
| 131 | + </FormattedString> |
| 132 | + </Label.FormattedText> |
| 133 | +</Label> |
| 134 | +``` |
| 135 | + |
| 136 | + |
| 137 | +#### DoubleHoursToTimeSpanConverter |
| 138 | +This `Converter` converts a `List<string>` into a single `String` with the defined separator char. |
| 139 | + |
| 140 | +#### LongToGigaByteConverter |
| 141 | +This `Converter` converts a `long` into a `Double`. The result wil be in GigaBytes. |
| 142 | + |
| 143 | +#### LongToMegaByteConverter |
| 144 | +This `Converter` converts a `long` into a `Double`. The result wil be in MegaBytes. |
| 145 | + |
| 146 | +#### StringToColorConverter |
| 147 | +This `Converter` converts a `String` (hex formated string) into a `Color`. |
| 148 | + |
| 149 | +#### UnixDateToDateTimeConverter |
| 150 | +This `Converter` converts a `Double` (UNIX based) into a `DateTime`. |
| 151 | + |
| 152 | +```xaml |
| 153 | +<Label |
| 154 | + LineBreakMode="WordWrap" Margin="2,10,0,10" |
| 155 | + VerticalTextAlignment="Center" |
| 156 | + FontSize="{OnIdiom Tablet=14, Default=12}" |
| 157 | + > |
| 158 | + <Label.FormattedText> |
| 159 | + <FormattedString> |
| 160 | + <Span Text="("/> |
| 161 | + <Span Text="{Binding CurrentDateTime, Converter={StaticResource UnixDateToDateTimeConverter}}"/> |
| 162 | + <Span Text=")"/> |
| 163 | + </FormattedString> |
| 164 | + </Label.FormattedText> |
| 165 | +</Label> |
| 166 | +``` |
| 167 | + |
| 168 | +#### UnixDoubleHoursToTimeSpanConverter |
| 169 | +This `Converter` converts a `Double` (UNIX based) into a `TimeSpan`. |
| 170 | + |
| 171 | +```xaml |
| 172 | +<Label |
| 173 | + LineBreakMode="WordWrap" Margin="2,10,0,10" |
| 174 | + VerticalTextAlignment="Center" |
| 175 | + FontSize="{OnIdiom Tablet=14, Default=12}" |
| 176 | + > |
| 177 | + <Label.FormattedText> |
| 178 | + <FormattedString> |
| 179 | + <Span Text="("/> |
| 180 | + <Span Text="{Binding RemainingPrintTime, Converter={StaticResource UnixDoubleHoursToTimeSpanConverter}}"/> |
| 181 | + <Span Text=")"/> |
| 182 | + </FormattedString> |
| 183 | + </Label.FormattedText> |
| 184 | +</Label> |
| 185 | +``` |
| 186 | + |
| 187 | +#### UriToStringConverter |
| 188 | +This `Converter` converts a `Uri` into a `String`. |
| 189 | + |
| 190 | +### Helpers |
| 191 | + |
| 192 | +#### Namespace |
| 193 | +```cs |
| 194 | +namespace AndreasReitberger.Shared.XForm |
| 195 | +``` |
| 196 | + |
| 197 | +#### ViewModelBase |
| 198 | +This `Class` contains all needed properties for a ViewModel. You can inherit from this Class directly for your ViewModel, |
| 199 | +or create an own ViewModelBase and inherit there form this class. |
| 200 | + |
| 201 | +```cs |
| 202 | +public partial class BaseViewModel : ViewModelBase |
| 203 | + { |
| 204 | + #region Properties |
| 205 | + |
| 206 | + #region App |
| 207 | + bool _isBeta = SettingsStaticDefault.App_IsBeta; |
| 208 | + public new bool IsBeta |
| 209 | + { |
| 210 | + get { return _isBeta; } |
| 211 | + set { SetProperty(ref _isBeta, value); } |
| 212 | + } |
| 213 | + bool _isLightVersion = SettingsStaticDefault.App_IsLightVersion; |
| 214 | + public bool IsLightVersion |
| 215 | + { |
| 216 | + get { return _isLightVersion; } |
| 217 | + set { SetProperty(ref _isLightVersion, value); } |
| 218 | + } |
| 219 | + |
| 220 | + bool _isTabletMode = false; |
| 221 | + public bool IsTabletMode |
| 222 | + { |
| 223 | + get { return _isTabletMode; } |
| 224 | + set { SetProperty(ref _isTabletMode, value); } |
| 225 | + } |
| 226 | + #endregion |
| 227 | + |
| 228 | + #region Navigation |
| 229 | + bool _isViewShown = false; |
| 230 | + public bool IsViewShown |
| 231 | + { |
| 232 | + get { return _isViewShown; } |
| 233 | + set { SetProperty(ref _isViewShown, value); } |
| 234 | + } |
| 235 | + #endregion |
| 236 | + |
| 237 | + #region Connection |
| 238 | + bool _isConnecting = false; |
| 239 | + public bool IsConnecting |
| 240 | + { |
| 241 | + get { return _isConnecting; } |
| 242 | + set { SetProperty(ref _isConnecting, value); } |
| 243 | + } |
| 244 | + #endregion |
| 245 | + |
| 246 | + #endregion |
| 247 | + |
| 248 | + #region Constructor |
| 249 | + public BaseViewModel() |
| 250 | + { |
| 251 | + |
| 252 | + } |
| 253 | + #endregion |
| 254 | + |
| 255 | + #region LiveCycle |
| 256 | + public void OnDisappearing() |
| 257 | + { |
| 258 | + try |
| 259 | + { |
| 260 | + if (SettingsApp.SettingsChanged) |
| 261 | + { |
| 262 | + // Notify other modules |
| 263 | + MessagingCenter.Send(new AppMessageInfo(), AppMessages.OnSettingsChanged.ToString()); |
| 264 | + SettingsApp.SaveSettings(); |
| 265 | + SettingsApp.SettingsChanged = false; |
| 266 | + } |
| 267 | + } |
| 268 | + catch (Exception exc) |
| 269 | + { |
| 270 | + // Log error |
| 271 | + EventManager.Instance.LogError(exc); |
| 272 | + } |
| 273 | + } |
| 274 | + #endregion |
| 275 | + } |
| 276 | +``` |
| 277 | + |
| 278 | +#### JsonConvertHelper |
| 279 | +This `Class` uses Newtonsoft.JSON in order to serialize and deserialize objects to strings and vice reverse. |
| 280 | +This is helpful if you want to store Collections or custom objects in the MAUI.Preferences (Settings). |
| 281 | + |
| 282 | +```cs |
| 283 | +// Convert to a String |
| 284 | +SettingsApp.WebCam_DefaultWebCamSettings = JsonConvertHelper.ToSettingsString(value); |
| 285 | + |
| 286 | +// Convert back to an object |
| 287 | +ObservableCollection<KlipperWebCamSettingsInfo> webcams = JsonConvertHelper.ToObject(SettingsApp.WebCam_Settings, new ObservableCollection<KlipperWebCamSettingsInfo>()); |
| 288 | +``` |
0 commit comments