0

Trying to get this solution to work, I can't figure out what is wrong with my implementation :

it is not clear to me how to "add the BuildDate.txt file as a resource" and how to access it from within my PCL ViewModel class :

I added a BuildDate.txt file into MyProject.Droid/Resources but I can't get its build action to "Android Resource" since it shows this error :

obj\Debug\res builddate.txt invalid resource directory name

The build is successful and effectively updates BuildDate.txt with "11/03/2018 19:01:29,11".

None of the referenced solutions has worked for me, but they are not Xamarin.Forms specific...

Has anyone already added and accessed successfully any text file resource in a Xamarin.Forms Android project ?

ting12
  • 81
  • 10
  • Can you explain how you got to the conclusion that the exception is at all related to the `ResourceDictionary`? `Resources` is a `Dictionary` and when you use `Application.Current.Resources["BuildDate"]` you are accessing it – Camilo Terevinto Mar 11 '18 at 23:46
  • Or use a MSBuild task, see [Get a Xamarin assembly build date on iOS, Android and OS X](https://stackoverflow.com/questions/37119859/get-a-xamarin-assembly-build-date-on-ios-android-and-os-x): – Benl Mar 15 '18 at 22:46
  • Camilo : I edited my post since my actual question is about any way of accessing this data... – ting12 Mar 20 '18 at 12:50
  • Benl : Not sure about the relevance of adding a whole MSBuild package just to have the build date displayed automatically in a View... – ting12 Mar 20 '18 at 12:53

1 Answers1

0

I have a solution that seems to work for me generally for creating a build date/time into a Xamarin.Forms app, if you're still interested.

I was confused about creating a resource file, and then thought-- maybe I could save a step. My idea was to create a SOURCE file in a pre-build step. To me, it seems simpler.

First, create a class in the SHARED project:

namespace MyProj {
    public partial class BuildDetails {
        static string buildDate = "build_date";

        public static string dispBuildDate() {
            getBuildDate();
            return buildDate;
        }
    static partial void getBuildDate();
}

}

Then, in the Android project properties, on the Build Events tab, put this in as the "Pre-build event command line:

echo namespace MyProj { >"$(ProjectDir)\BuildDetails.cs
echo public partial class BuildDetails {  >>"$(ProjectDir)\BuildDetails.cs
echo static partial void getBuildDate() { >>"$(ProjectDir)\BuildDetails.cs
echo    buildDate = "%date% %time%" ; >>"$(ProjectDir)\BuildDetails.cs
echo } } }  >>"$(ProjectDir)\BuildDetails.cs

This is a bit ugly, but it seems to work for me (and,should work for all project types).

bobwki
  • 794
  • 6
  • 22
  • Wow! Was looking this up again, and ... didn't recognize it was my own answer. STILL WORKS (and, STILL UGLY). – bobwki Jul 08 '23 at 00:07
  • Oh, and I forgot -- make a fake "BuildDetails.cs" file and add it to the .Android project. (presuming it's a SHARED PROJECT style...) – bobwki Jul 08 '23 at 00:13